简体   繁体   English

使用CoreData插入数千个数据时,iOS App崩溃

[英]iOS App crash when inserting thousands of data with CoreData

Actually, I have implemented background sync call which stores data in a local database using CoreData. 实际上,我已经实现了后台同步调用,该调用使用CoreData将数据存储在本地数据库中。 I am getting thousands of records for that, but the app crashes after some time and giving the error 我为此获得了数千条记录,但是一段时间后应用崩溃了,并给出了错误信息

EXC_BAD_ACCESS Code 1. EXC_BAD_ACCESS代码1。

Maybe it happened due to NSManagedObjectContext , not sure. 可能是由于NSManagedObjectContext导致的,不确定。 Here is my code: 这是我的代码:

-(void)RestClient:(RestClient *)client didCompleteWithSuccess:(id)responseObject withLastModifiedDate:(NSString *)lastModifiedDate{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

    @try {

        if(responseObject!=nil){

            NSError *error = nil;
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
            if (!error) {

                int totalrecords = [[jsonResponse objectForKey:@"records"] intValue];

                if(totalrecords>0){
                    NSManagedObjectContext * manageObjContext = [Common getBkgManagedObjectContext];
                    NSArray* rows = [jsonResponse objectForKey:@"rows"];

                    for(NSString* email in rows){
                        [NotifyEmails insertIntoNotifyEmails:email withManagedObjectContext:manageObjContext];
                    }

                    NSError *error = nil;
                    if (![manageObjContext save:&error]) {
                        NSLog(@"Data couldn't save: %@", [error localizedDescription]);
                    }


                }
            }
        }

    }@catch (NSException *exception) {
        NSLog(@"exception in rest response");
        NSLog(@"Error :: %@",exception);
    }@finally {
        [self finishOperation];
    }

});
}

The NsmanagedObjectContext function : NsmanagedObjectContext函数:

- (NSManagedObjectContext *)getBkgManagedObjectContext {

    NSManagedObjectContext *manageObjCtx;
    NSPersistentStoreCoordinator *coordinator = [(EMAINTAINAppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator];

    if (coordinator != nil) {
        manageObjCtx = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [manageObjCtx setPersistentStoreCoordinator:coordinator];
    }

    return manageObjCtx;

}

ManagedObjectContext are not thread safe. ManagedObjectContext不是线程安全的。 It is vital to know for each context which thread it want to be run on. 了解每个上下文要在哪个线程上运行至关重要。 A context that was created with NSPrivateQueueConcurrencyType comes bundled with a private thread that you can ONLY access with performBlock or performBlockAndWait (in other words the queue is PRIVATE). 使用NSPrivateQueueConcurrencyType创建的上下文捆绑了一个私有线程,您只能使用performBlockperformBlockAndWait (换句话说,队列是PRIVATE)进行访问。 It is not safe to access the context on any other thread - even a background thread. 在任何其他线程(甚至是后台线程)上访问上下文都是不安全的。 The context is not attached to the thread that it was created on (as you seem to be assuming it will be), that was the behavior of NSConfinementConcurrencyType which is now deprecated. 上下文没有附加到创建它的线程上(就像您假设的那样),这是现在已弃用的NSConfinementConcurrencyType的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM