将多个上下文与核心数据一起使用时出现错误133000 [英] Error 133000 when using multiple contexts with core data

查看:93
本文介绍了将多个上下文与核心数据一起使用时出现错误133000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了几天时间尝试解决这个问题的所有可能的解决方案,但是似乎没有任何效果。

I've spent days trying every possible solution I can think of to this problem, but nothing seems to be working.

我运行这样的后台线程:

I run a background thread like this:

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {

                    Media *localMedia = [media inContext:localContext];

                    UIImage *image = localMedia.thumbnail;


                    dispatch_async(dispatch_get_main_queue(), ^{

                        [thumbnails setObject:image forKey:[NSNumber numberWithInt:i]];
                        [contentDict setObject:thumbnails forKey:@"MediaItems"];
                        [cell.entryView setNeedsDisplay];
                    });

                }];

或者像这样:

dispatch_queue_t cellSetupQueue = dispatch_queue_create("com.Journalized.SetupTimelineThumbnails", NULL);
            dispatch_async(cellSetupQueue, ^{

                NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
                NSPersistentStoreCoordinator *coordinator = [NSManagedObjectContext contextForCurrentThread].persistentStoreCoordinator;
                [newMoc setPersistentStoreCoordinator:coordinator];

                NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
                [notify addObserver:self
                           selector:@selector(mergeChanges:)
                               name:NSManagedObjectContextDidSaveNotification
                             object:newMoc];

Media *localMedia = [media inContext:[NSManagedObjectContext contextForCurrentThread];

                        UIImage *image = localMedia.thumbnail;


                        dispatch_async(dispatch_get_main_queue(), ^{

                            [thumbnails setObject:image forKey:[NSNumber numberWithInt:i]];
                            [contentDict setObject:thumbnails forKey:@"MediaItems"];
                            [cell.entryView setNeedsDisplay];
                        });

                    }];

这两个都使我崩溃,UIImage返回为nil对象,而 Cocoa错误133000

Both of these give me a crash with UIImage returning as nil object, and a Cocoa Error 133000.

我删除了所有其他后台线程代码,并在此之前直接保存在主线程中确保;确定。在主线程上运行上面的代码也可以,但是冻结了我的UI。尽管进行了所有这些努力,以上代码每次都崩溃。

I've removed every other piece of background threading code, and have saved on the main thread directly before this just to make sure. Running the code above on the main thread also works, but freezes up my UI. Despite all of these efforts, the above code crashes every time.

我不确定该怎么做。

编辑:

我的问题是,如何使这项工作正常进行而不会崩溃?来自1个上下文的对象似乎不存在于另一个上下文中,这似乎是一个问题,但是我如何使它们存在于另一个上下文中?

My question, specifically, is how do I make this work without crashing? It seems to be a problem with objects from 1 context not existing in another, but how do i make them exist in another context?

推荐答案

<请记住,MR_inContext:方法在幕后使用[NSManagedObjectContext objectWithID:]方法。您应该查看那里以确保您的对象具有:

Remember, the MR_inContext: method is using [NSManagedObjectContext objectWithID: ] method under the covers. You should look in there to make sure your object has:

1)在进入第一个代码块
2的背景上下文/块之前已保存)此方法返回了一些有用的信息

1) Been saved prior to entering into the background context/block in your first code block 2) This method is returning something useful

我也不确定如何设置缩略图属性。理想情况下,只要编写了NSTransformable代码就没关系(互联网上有一些示例向您展示了如何使用transformable属性将UIImage保存在核心数据中)

I'm also not sure how you set up your thumbnail attribute. Ideally it shouldn't matter as long as you have the NSTransformable code write (there are samples on the internets that show you how to save a UIImage in core data using the transformable attribute)

此外,您的代码应如下所示:

Also, your code should look like this:

UIImage *image = nil;
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {

  Media *localMedia = [media inContext:localContext]; //remember, this looks up an existing object. If your context is a child of a parent context that has contains this, the localContext should refresh the object from the parent.

  //image processing/etc

  image = localMedia.thumbnail;

} completion:^{              

    [thumbnails setObject:image forKey:@(i)]; //new literals are teh awesome
    contentInfo[@"MediaItems"] = thumbnails;  //as is the new indexer syntax (on the latest compiler)

    [cell.entryView setNeedsDisplay];

}];

这篇关于将多个上下文与核心数据一起使用时出现错误133000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆