Q:

Using Core Data in IOS6 (delete/edit)

I am saving the latest internet request of my tableviewdata in an (core data) entity, but have problems with error exceptions about "faults".
I have two methods 'loadData' which gets the latest 'ordersitems' that will be loaded in my tableview AND 'loadThumbnails' which will try to cache the thumbnail into the core data entity.
The problem occurs when the managedobject gets deleted and the thumbnail method still tries to access it. Though i made a variable stopThumbnails to stop the loadThumbnails method, the problem keeps occurring.
What is the proper iOS 6 way to lazyload the images and save them to coredata but check if the object has not been deleted? i found this Core Data multi thread application which was useful but my newbie understanding of core data is still limited and i have problems writing code. I read the apple docs about http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/Articles/cdConcurrency.html but it was hard to understand completely.
I want at least my http request to load asychronous (but preferably as much as possible) i came up with the following:
-(void)viewdidload
{
     NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"OrderItems"];
    fetchReq.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];                    
    self.data = [self.managedObjectContext executeFetchRequest:fetchReq error:nil];

     MYFILTER = @"filter=companyX";

     [self loadData];
}

-(void)loadData
{
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //json request from url
        NSDictionary *reqData = myOrderJSONRequest(MYFILTER);

        dispatch_async( dispatch_get_main_queue(), ^{

            if(reqData!=NULL && reqData!=nil)
            {
                //request successful so delete all items from entity before inserting new ones

                stopThumbnails = YES;
                for(int i=self.data.count-1;i>=0;i--)
                {
                   [self.managedObjectContext deleteObject:[self.data objectAtIndex:i]];
                }
                [self.managedObjectContext save:nil];

                if(reqData.count>0)
                {
                    //insert latest updates           
                    for (NSDictionary *row in reqData){
                        OrderItem *item = [NSEntityDescription insertNewObjectForEntityForName:@"OrderItem" inManagedObjectContext:self.managedObjectContext];
                        item.order_id =  [NSNumber numberWithInt:[[row objectForKey:@"order_id"] intValue]];
                        item.description = [row objectForKey:@"description"];
                        item.thumbnail_url = [row objectForKey:@"thumbnail_url"];
                    }
                    [self.managedObjectContext save:nil];
                }

                NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"OrderItems"];
                fetchReq.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];                    
                self.data = [self.managedObjectContext executeFetchRequest:fetchReq error:nil];                    

                [TableView reloadData];

                //LOAD THUMBNAILS ASYNCHRONOUS
                stopThumbnails = NO;
                [self loadThumbnails];                    
            }
            else{
                //NO INTERNET
            }
        });
    });
}

-(void)loadThumbnails
{
if(!loadingThumbnails)
{
    loadingThumbnails = YES;
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        for (int i=0;i<self.data.count; i++) {
            if(!stopThumbnails)
            {
                OrderItem *item = [self.data objectAtIndex:i];
                if(item.thumbnail==NULL)
                {
                    //ASYNCHRONOUS IMAGE REQUEST
                    NSURL *image_url = [NSURL URLWithString:item.thumbnail_url];
                    NSData *image_data = [NSData dataWithContentsOfURL:image_url];

                    dispatch_async( dispatch_get_main_queue(), ^{
                        if(image_data!=nil && image_data!=NULL && !stopThumbnails)
                        {
                            //IMAGE REQUEST SUCCESSFUL
                            item.thumbnail = image_data;
                            [self.managedObjectContext save:nil];

                            //RELOAD AFFECTED TABLEVIEWCELL
                            NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:i inSection:0];
                            NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
                            [TableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationFade];
                        }
                        else
                        {
                            loadingThumbnails = NO;
                            return;
                        }
                    });
                }

                if(stopThumbnails)
                {
                    dispatch_async( dispatch_get_main_queue(), ^{
                        loadingThumbnails = NO;
                        return;
                    });
                }
            }
            else{
                dispatch_async( dispatch_get_main_queue(), ^{
                    loadingThumbnails = NO;
                    return;
                });
            }
        }
        dispatch_async( dispatch_get_main_queue(), ^{
            loadingThumbnails = NO;
            return;
        });
    });
}
}

Any help is of course greatly appreciated :)

A:

Well i dont know if this is the right approach but it works, so i'll mark this as an answer.
To do everything on the background i used a second nsmanagedobjectcontext (MOC) and then merge the changes to the main MOC. the dispatch queue works great although i had to use the NSManagedObjectContextDidSaveNotification in order to merge the changes of the two contexts.
since IOS 5 its possible to use blocks instead that do the merging for you. So i decided to use this instead of the dispatch way (this way i didnt have to use notofications).
Also using blocks i got the same problem (faults) when an object got selected on a background queue while is was deleted on a different queue. So i decided instead of deleting it right away, insert a NSDate 'deleted' property for the OrderItem. then have a timer with a delete check to see if there are objects that have been deleted longer than 10 minutes ago. This way i am sure no thumbnail was still downloading. It works. Though i would still like to know id this is the right approach :)

