ios – 核心数据’声明仍然有效’

前端之家收集整理的这篇文章主要介绍了ios – 核心数据’声明仍然有效’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对这个核心数据错误感到很不舒服.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason:     'statement is still active'

我的应用程序和代码一切正常,除非偶尔调用请求很快.它发生在我试图打破应用程序时.
从一个屏幕到下一个屏幕,下载数据并执行获取请求.

我知道这与线程和核心数据有关.

我从后台线程调用这段代码,它有自己的托管对象上下文.

+ (AN_User *)updateWithRecord:(NSDictionary *)record moc:(NSManagedObjectContext *)moc{

    NSNumber *userID = nil;
    NSString *username = nil;

    if([record objectForKey:@"user_id"]){
        userID = [NSNumber numberWithInt:[[record objectForKey:@"user_id"] intValue]];
    }else if([record objectForKey:@"id_member"]){
        userID = [NSNumber numberWithInt:[[record objectForKey:@"id_member"] intValue]];
    }

    if([record objectForKey:@"username"]){
        username = [NSString stringWithFormat:@"%@",[record objectForKey:@"username"]];
    }else if([record objectForKey:@"member_name"]){
        username = [NSString stringWithFormat:@"%@",[record objectForKey:@"member_name"]];
    }

    if(!userID||!username){
        return nil;
    }

    __block AN_User *user = nil;

    [moc performBlockAndWait:^{

        NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"AN_User" inManagedObjectContext:moc];
        [request setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(user_id == %@) OR (username == %@)",userID,username];
        [request setPredicate:predicate];

        if([moc countForFetchRequest:request error:nil]==0){
            user = (AN_User *)[NSEntityDescription insertNewObjectForEntityForName:@"AN_User" inManagedObjectContext:moc];
        }else{
            NSArray *fetchResults = [moc executeFetchRequest:request error:nil];
            if(fetchResults.count>0){
                user = [fetchResults objectAtIndex:0];
            }
        }

        if(user){

            user.user_id = userID;
            user.username = username.lowercaseString;

            //Parse profile image url
            NSString *avatar = [record objectForKey:@"avatar"];
            NSString *fileName = [record objectForKey:@"filename"];
            if([avatar isKindOfClass:[NSString class]]&&avatar.length>0){
                user.profile_image_url = [NSString stringWithFormat:@"%@",avatar];
            }else if([fileName isKindOfClass:[NSString class]]&&fileName.length>0){
                user.profile_image_url = [NSString stringWithFormat:@"http://www.example.com/forum/avs/%@",fileName];
            }

            if([record objectForKey:@"gpbp_respect"]){
                user.respect = [NSNumber numberWithFloat:[[record objectForKey:@"gpbp_respect"] floatValue]];
            }

        }
    }];
    return user;
}

我知道这可能很难说,但是有人能告诉我,如果我做错了,这些请求,这是显而易见的.

解决方法

如果您滚动一个在b / g线程上调用核心数据的表,它就会发生,并且Core Data希望在一个线程上拥有所有上下文.

另一个SO海报通过为每个线程创建一个MOContext来解决这个问题,但是我不喜欢在多个线程上使用CRUD的想法所以我只是在我的代码周围放了一个dispatch_async(dispatch_get_main_queue(),)包装函数.到目前为止还没有崩溃,但这种情况很少见,所以我对此并不十分肯定.

原文链接:https://www.f2er.com/iOS/333563.html

猜你在找的iOS相关文章