我正在尝试从包含数千个条目的 JSON 文件中填充 CoreData。我要做的是创建一个新的 NSManagedObjectContext,并将父属性设置为我的主 NSManagedObjectContext。我经常保存我的子上下文,如果成功,则保存我的主要上下文。如果这也成功,我会尝试重新加载我的 tableview。我的 CoreDataTableView 是使用 NSFetchedResultController 设置的,并直接取自 Apple 的文档。但是,表格视图不会刷新。有没有人有什么建议?
#pragma mark - Setters
- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
_managedObjectContext = managedObjectContext;
if (managedObjectContext) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Place"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"location" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
request.predicate = nil; //Fetch all Tags
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"group"
cacheName:nil];
}
}
#pragma mark - View Lifecycle
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.managedObjectContext) {
[self setupDocument];
}
}
- (void)parseList
{
dispatch_queue_t jsonParsing = dispatch_queue_create("json parsing", NULL);
dispatch_async(jsonParsing, ^{
//omitted for brevity
NSArray *places = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&parseError];
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = self.managedObjectContext;
int placesCount = [places count];
[childContext performBlock:^{
int count = 0;
for (NSDictionary *place in places)
{
//omitted for brevity. Calls a category on my Place NSManagedObjectSubclass passing in the child context
count++;
if (count % 1000 == 0 || count == placesCount) {
NSError *error;
if ([childContext save:&error]) {
NSLog(@"Saved On Child Context");
[self.managedObjectContext performBlock:^{
NSError *mainError;
if ([self.managedObjectContext save:&mainError]) {
NSLog(@"Saved On on Parent Context");
[self.tableView reloadData];
}
}]; //End of ManagedObject Block
}
}
}
}]; //End of ChildContext Block
}); //End of Json Parsing Block
}
- (void)setupDocument
{
NSURL *filePath = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
filePath = [filePath URLByAppendingPathComponent:@"Document"];
UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:filePath];
//Create if it doesn't exist
if (![[NSFileManager defaultManager] fileExistsAtPath:[filePath path]]) {
//Async save
[document saveToURL:filePath forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
self.managedObjectContext = document.managedObjectContext;
[self parseList];
}
}];
} else if (document.documentState == UIDocumentStateClosed){
[document openWithCompletionHandler:^(BOOL success){
//Open it, don't need to refetch stuff
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
} else {
//else use it the managed context
self.managedObjectContext = document.managedObjectContext;
}
}