0

I know I'm missing quite a bit here, but I can't find any tutorials with code and it's been driving me nuts.

I have an app that shows items in a table view (pulled from core data) that can be updated via pull-to-refresh. The updater pulls new data from a REST API and stores it in core data, and refreshes the master set of data contained within my TableViewController.

Obviously I would like the pull-to-refresh to be non-blocking, and so I've implemented it within a MOC:performBlock method call. Everything seems to work but I'm randomly getting application crashes when performing database operations within my model and I know it's because I'm managing my MOCs and persistent object model incorrectly. If anyone could point me in the right direction I'd be grateful.

Here's the method that actually performs the data and table refresh. Below I will list a bunch of auxiliary code that may be useful:

-(void)refresh {

    NSManagedObjectContext *child = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [child setParentContext:self.db];

    [child performBlock:^{
        model = [[CustomersModel alloc] init];
        self.custs = [model getUpdatedCustomers:self.db];
        self.workingSet = self.custs;
        dispatch_async(dispatch_get_main_queue(), ^{

            [super performSelector:@selector(stopLoading) withObject:nil afterDelay:1.0];
        });
    }];
}

-(void)stopLoadingComplete {

    ...
    [self.tableView reloadData];
}

Here's my appdelegate code for setting up MOC, persistent data store etc:

- (NSManagedObjectContext *)managedObjectContext
{

    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

Here's my app delegate code for assigning MOC to UITableViewControllers:

CustomerListViewController *clvc = [[CustomerListViewController alloc] initWithNibName:@"CustomerListViewController" bundle:nil withManagedContext:self.managedObjectContext];
clvc.detailViewController = detailViewController;
UINavigationController *clvcNav = [[UINavigationController alloc] initWithRootViewController:clvc];

Here's my customer list view controller's code for assigning MOC:

customerlistview.h:

#import <UIKit/UIKit.h>
#import "CustomersModel.h"
#import "SearchBarTableViewController.h"

@interface CustomerListViewController : SearchBarTableViewController
{
    CustomersModel *model;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withManagedContext:(NSManagedObjectContext *)db;

@property (retain) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic)NSManagedObjectContext *db;
@property (strong, nonatomic)NSArray *custs, *workingSet;

@end

customerlistview.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withManagedContext:(NSManagedObjectContext *)passedDB {

    self.db = passedDB;
    return [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
4

1 回答 1

0

我确实要回答这个问题——因为在子上下文完成它的操作之后保存父上下文,我还没有收到一个程序崩溃。

于 2012-11-03T13:22:40.313 回答