Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 133000 when using multiple contexts with core data

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

                }];

Or like this:

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

                    }];

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

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.

I'm not sure what to do to make this work.

Edit:

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?

like image 406
Andrew Avatar asked Jul 12 '26 21:07

Andrew


1 Answers

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

}];
like image 70
casademora Avatar answered Jul 15 '26 11:07

casademora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!