2012-07-12 106 views
0

我試圖選擇名爲「selectedPractice」的名爲「Practice」的名爲「Practice」的對象,名爲SelectorViewController的視圖控制器上,然後保留在另一個名爲RegularViewController的控制器中使用的結果。兩者都是UIViewControllers,因爲我試圖追加核心數據的現有應用程序不使用Tab或NavigationControllers。ViewControllers之間傳遞NSFetchedResultsController

我嘗試使用以下代碼來設置兩個控制器的ManagedObjectContexts是相同的AppDelegate在AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
SelectorViewController *svc; 
    svc.managedObjectContext = self.managedObjectContext; 
    svc.fetchedResultsController = self.fetchedResultsController; 

    RegularViewController *rvc; 
    rvc.managedObjectContext = self.managedObjectContext; 
    rvc.fetchedResultsController = self.fetchedResultsController; 
     return YES; 
} 

AppDelegate.m還具有setupFetchedResultsController方法這裏(改編自Tim Roadley的出色工作):

- (void)setupFetchedResultsController 
{ 
    // 1 - Decide what Entity you want 
    NSString *entityName = @"Practice"; // Put your entity name here 
    NSLog(@"AppDelegate Setting up a Fetched Results Controller for the Entity named %@", entityName); 

    // 2 - Request that Entity 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; 

    // 3 - Filter it if you want 
    //request.predicate = [NSPredicate predicateWithFormat:@"Practice.name = Blah"]; 

    // 4 - Sort it if you want 
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" 
                        ascending:YES 
                         selector:@selector(localizedCaseInsensitiveCompare:)]]; 

    // 5 - Fetch it 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                     managedObjectContext:self.managedObjectContext 
                      sectionNameKeyPath:nil 
                        cacheName:nil]; 
    [self.fetchedResultsController performFetch:nil]; 
    NSLog(@"AppDelegate setupFetchedResultsController completed"); 
} 

這個運行正常,所有NSLogs顯示。 SelectorViewController.m中有setupFetchedResultsController的另一個實例,但它會導致下列日誌崩潰。 FetchedResultsController是否正在重新啓動?

2012-07-12 20:49:07.652 WhiteHealth [6969:15b03] SelectorViewController 排序實體完整2012-07-12 20:49:07.653 WhiteHealth [6969:15b03] *終止應用程序,由於未捕獲的異常 'NSInvalidArgumentException' 的,理由是: ' NSFetchedResultsController的一個實例都需要一個非空fetchRequest和 managedObjectContext'

- (void)performFetch 
{ 
    if (self.fetchedResultsController) { 
     if (self.fetchedResultsController.fetchRequest.predicate) { 
      if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate); 
     } else { 
      if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName); 
     } 
     NSError *error; 
     [self.fetchedResultsController performFetch:&error]; 
     if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]); 
    } else { 
     if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); 
    } 
    [pickerView reloadAllComponents]; 
} 

- (void)setupFetchedResultsController 
{ 
    // 1 - Decide what Entity you want 
    NSString *entityName = @"Practice"; // Put your entity name here 
    NSLog(@"SelectorViewController Setting up a Fetched Results Controller for the Entity named %@", entityName); 

    // 2 - Request that Entity 
    [NSFetchedResultsController deleteCacheWithName:nil]; 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; 
    NSLog(@"SelectorViewController requesting entity complete"); 

    // 3 - Filter it if you want 
    //request.predicate = [NSPredicate predicateWithFormat:@"Person.name = Blah"]; 

    // 4 - Sort it if you want 
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" 
                        ascending:YES 
                         selector:@selector(localizedCaseInsensitiveCompare:)]]; 
    NSLog(@"SelectorViewController sorting entity complete"); 

    // 5 - Fetch it 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                     managedObjectContext:self.managedObjectContext 
                      sectionNameKeyPath:nil 
                        cacheName:nil]; 
    [self performFetch]; 

} 

回答

3
SelectorViewController *svc; 
svc.managedObjectContext = self.managedObjectContext; 
svc.fetchedResultsController = self.fetchedResultsController; 

RegularViewController *rvc; 
rvc.managedObjectContext = self.managedObjectContext; 
rvc.fetchedResultsController = self.fetchedResultsController; 
    return YES; 

不會爲你設置managedObjectContexts,你不初始化svc和rvc,你所做的只是創建對象。如果你想設置managedObject上下文,進入viewDidLoad's或者在你運行提取請求之前,並且執行以下檢查:

if (self.managedObjectContext == nil) { 
    self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
} 
+0

這很好用 - 非常感謝。 – 2012-07-13 14:27:54

相關問題