我正在尝试从包含数千个条目的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;
}
}
答案 0 :(得分:0)
数据是否已保存?如果数据实际上正在保存但表格没有反映,请尝试这样做:
[tableView reloadData]
答案 1 :(得分:0)
我相信这段代码需要在主线程上执行......
[self.managedObjectContext performBlock:^{
NSError *mainError;
if ([self.managedObjectContext save:&mainError]) {
NSLog(@"Saved On on Parent Context");
[self.tableView reloadData];
}
}]; //End of ManagedObject Block
Try wrapping it in a:
dispatch_async(dispatch_get_main_queue(), ^{
}