execute fetchrequest causes crash

来源:互联网 发布:软件测试的发展史 编辑:程序博客网 时间:2024/06/02 02:09

NSManagedObjectContext is not thread safe. If you creating context on main thread, then you can access the context only on the main thread


So you have to run the executeFetchRequest in the main thread, instead you can use performBlock which will schedule it to run on its own thread.

  //performblock将安排它运行在它自己的线程

- (NSArray *)executeFetchRequest:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context
{
    __block NSArray *results = nil;
    [context performBlockAndWait:^{
        NSError *error = nil;
        results = [context executeFetchRequest:request error:&error];
    }];
    return results;
}

performBlock won't work if you're initialising the context in the older NSConfinementConcurrencyType confinement model.

0 0
原创粉丝点击