3

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

我运行这样的后台线程:

[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 Error 133000.

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

我不知道该怎么做才能完成这项工作。

编辑:

具体来说,我的问题是如何在不崩溃的情况下完成这项工作?来自 1 个上下文的对象在另一个上下文中不存在似乎是一个问题,但是我如何使它们存在于另一个上下文中?

4

2 回答 2

2

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) Been saved prior to entering into the background context/block in your first code block 2) This method is returning something useful

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];

}];
于 2012-08-03T15:52:46.427 回答
1

快速回答:

NSManagedObjectReferentialIntegrityError = 133000, // 尝试触发指向不存在的对象的故障(我们可以看到存储,我们看不到对象)

编辑:很难从代码中看到一些东西。那里的托管对象是什么?

我想问题是您在另一个上下文中使用来自一个上下文的临时对象。

于 2012-08-03T13:38:10.040 回答