我有一个像这样的coredata模型设置:
TileList <->> TileListOrder
TileListOrder <<-> Tile
Run Code Online (Sandbox Code Playgroud)
并创建了一个NSFetchedResultsController:
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"tile <> nil AND tileList <> nil AND tile.removed == 0 AND tileList.removed == 0"];
NSSortDescriptor* sortTileListDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"tileList.order" ascending:YES] autorelease];
NSSortDescriptor* sortTileDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES] autorelease];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:sortDescriptors];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TileListOrder" inManagedObjectContext:coreManagedObjectContext];
[fetchRequest setEntity:entity];
NSFetchedResultsController* fetchedResults = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:coreManagedObjectContext
sectionNameKeyPath:@"tileList.order"
cacheName:nil];
Run Code Online (Sandbox Code Playgroud)
我还在辅助NSManagedObjectContext上导入数据并将这些更改合并回来,如下所示:
- (void) localContextDidSave:(NSNotification*) notification {
dispatch_sync(dispatch_get_main_queue(), ^{
[coreManagedObjectContext mergeChangesFromContextDidSaveNotification:notification]; …Run Code Online (Sandbox Code Playgroud)