Output basic data based on properties of "ordered" relationships

My application has an intelligent folder, such as functionality: the predicate is configured using NSPredicateEditor and is used to populate the folder with a select query.

The object used in the search is to-many related. The relationship is ordered in the sense that the index is stored in the target for sorting purposes.

My problem is that I would like to build a rule based on the latest values ​​in an ordered relation, but I cannot figure out how to construct a predicate for this, because the relation is not an array. The master data is virtually unaware of the order.

I have a readonly property in a class that returns ordered elements, but this does not look like a select query because the property is not available in the master data store.

The only option I can think of is to de-normalize and preserve the last elements in relationships ordered in a separate property. Is this the only solution?

+3
source share
2 answers

Well, believing that I understood the problem correctly, I would do it like this. Suppose you have two objects, TopEntity has a name property (NSString *) and a to-many relation to MyEntity that has a data property (NSString *) and (NSInteger).

, TopEntity, , MyEntity , NSFetchRequest, ...

NSManagedObjectContext *context = [self managedObjectContext];

// Create some top level entities
TopEntity *aTop = [TopEntity insertInManagedObjectContext:context];
aTop.name = @"This is Your Name";
TopEntity *bTop = [TopEntity insertInManagedObjectContext:context];
bTop.name = @"This aint a Name";    
TopEntity *cTop = [TopEntity insertInManagedObjectContext:context];
cTop.name = @"This is My Name";    

// Add some data
NSInteger i, len = 30;
for(i=0; i<len; i++) {
    // Create a new object
    MyEntity *entity = [MyEntity insertInManagedObjectContext:context];
    entity.orderValue = i;
    entity.data = [NSString stringWithFormat:@"This is some data: %d", i];
    if(i < 10) {
        [aTop addObjectsObject:entity];
        [entity addTopObject:aTop];
    } else if (i < 20) {
        [bTop addObjectsObject:entity];
        [entity addTopObject:bTop];            
    } else {
        [cTop addObjectsObject:entity];
        [entity addTopObject:cTop];                        
    }
}

// Save the context
NSError *error = nil;
[context save:&error];

// A predicate to match against the top objects
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@", @"This is"];
// A predicate to match against the to-many objects
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"ANY objects.order < %d", 5];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"TopEntity" inManagedObjectContext:context]];
[fetch setPredicate:predicate];
NSArray *result = [[context executeFetchRequest:fetch error:&error] filteredArrayUsingPredicate:secondPredicate];


for(TopEntity *entity in result) {
    NSLog(@"entity name: %@", entity.name);         
}

, , ANY.

, , . " ", TopEntity.

+2

, n , .

n , , "lastN" (, -and-drop).

, , ( -setFetchLimit:) n .

"", (- ). , , , , . .: -)

+1

Source: https://habr.com/ru/post/1777121/


All Articles