Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在核心数据中导入大型数据集时更新UI_Ios_Objective C_Multithreading_User Interface_Core Data - Fatal编程技术网

Ios 在核心数据中导入大型数据集时更新UI

Ios 在核心数据中导入大型数据集时更新UI,ios,objective-c,multithreading,user-interface,core-data,Ios,Objective C,Multithreading,User Interface,Core Data,在我的应用程序中,我有一个功能,可以将大型数据集从服务器下载到核心数据中。它看起来像这样: for (PFObject *historyObject in historyArray) { History *history = (History *)[NSEntityDescription insertNewObjectForEntityForName:@"History" inManagedObjectContext:managedObjectContext]; history.

在我的应用程序中,我有一个功能,可以将大型数据集从服务器下载到核心数据中。它看起来像这样:

for (PFObject *historyObject in historyArray) {
    History *history = (History *)[NSEntityDescription insertNewObjectForEntityForName:@"History" inManagedObjectContext:managedObjectContext];
    history.date = historyObject.date;
    // ...
}
因为这可能需要几分钟的时间,所以我想在此过程中更新UI,并向用户显示已经取得的进展。 现在,因为将这些数据插入核心数据会阻塞主线程,所以我不确定是否有其他合适的方法来更新UI

我尝试了几种方法将插入移动到不同的线程中,但在核心日期响应时总是出现错误

我希望有人有一个好主意,并愿意与大家分享。
非常感谢您的帮助

我目前已通过使用并发类型NSPrivateQueueConcurrencyType初始化ManagedObjectContext并将父上下文设置为主ManagedObjectContext来解决此问题。 对于有相同问题的任何人:

- (void)doSomething
{
    _backgroundMOC = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [_backgroundMOC setParentContext:[kDelegate managedObjectContext]];

    for (int i = 0; i < [cars count]; i++)
    {
        [_backgroundMOC performBlockAndWait:^{

            Drive *drive = (Drive *)[NSEntityDescription insertNewObjectForEntityForName:@"Drive" inManagedObjectContext:_backgroundMOC.parentContext];

            ....do more stuff...

        }];
    }

    [self performSelectorOnMainThread:@selector(showProgress:) withObject:[NSNumber numberWithFloat:((float)i/(float)[cars count])] waitUntilDone:NO];
}

你能展示一下当你转到背景线程时你尝试了什么吗?作为警告,如果您以任何方式阻止主线程,您的应用程序将被拒绝。我认为我们尝试了performSelectorInBackground:我建议您阅读一些关于nsrunlop类的内容,它可以在该场景中帮助您。您必须共享详细信息。您有什么类型的错误?您使用的是哪种并发模型,传统的还是父子的?我不同意这里所有的评论。核心数据是一个非常强大的框架,特别是在多线程环境中使用时。如果我是你,我会看看GitHub上的MagicRecord,因为它们是最快的解决方案。否则,您可以将限制类型设置为Private来设置保存上下文
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) 
    {
    return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) 
    {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];

    }

    return _managedObjectContext;
}