2011-07-08 55 views
0

關係擷取實體我在我的核心數據的數據庫Person 2個實體和Photo一個PersonPhoto一個To-Many關係,PhotoPerson(Person<-->>Photo)反向關係。我通過以下方式將我的應用程序的一些初始數據加載到.plist中:與核心數據

-(void)populateDataStorage{ 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"]; 

    if(path){ 
     NSArray *plistData = [[NSArray alloc] initWithContentsOfFile:path]; 
     NSEnumerator *enumerator = [plistData objectEnumerator]; 

     NSArray *personResults; 

     Photo *photo; 
     Person *person; 

     id currItem = [enumerator nextObject]; 

     while (currItem != nil) { 
      photo = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:[flickrFetcher managedObjectContext]]; 

      photo.name = [currItem objectForKey:@"name"]; 
      photo.path = [currItem objectForKey:@"path"]; 

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", [currItem objectForKey:@"user"]]; 

      personResults = [flickrFetcher fetchManagedObjectsForEntity:@"Person" withPredicate:predicate]; 

      if ([personResults count] > 0) { 
       person = [personResults objectAtIndex:0]; 
      } 
      else { 
       person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[flickrFetcher managedObjectContext]]; 
       person.name = [currItem objectForKey:@"user"]; 
      } 

      photo.person = person; 
      [person addPhotosObject:photo]; 

      NSLog(@"Photo %@ added for user %@", photo.name, person.name); 

      currItem = [enumerator nextObject]; 
     } 

     [plistData release]; 

    } 

} 

一切正常加載。然後我使用NSFetchedResultsController獲取Person並加載UITableView,這一切都可行。當用戶點擊該屬於用戶的小區我必須加載另一個UITableView與選定人員的照片,這樣我就在我的UITableViewController我的照片的表視圖如下:

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person = %@", person]; 
     self.fetchedResultsController = [[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Photo" withPredicate:predicate]; 
    } 
    return self; 
} 

在我viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSError *error = nil; 
    [fetchedResultsController performFetch:&error]; 

    for(Photo *photo in person.photos){ 
     NSLog(@"Photo %@ belongs to %@", photo.name, person.name); 
    } 

    NSLog(@"Photo count %d", [[fetchedResultsController fetchedObjects] count]); 
} 

計數是給我的0,我從來沒有這個人的照片,但我知道他們是正確保存,因爲它們都打印出在Person傳遞。任何想法我可能做錯了什麼?

在我FlickrFetcher類獲得NSFetchedResultsController編輯

實用方法:

- (NSFetchedResultsController *)fetchedResultsControllerForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate { 
    NSFetchedResultsController *fetchedResultsController; 

    /* 
    Set up the fetched results controller. 
    */ 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:20]; 

    // Edit the sort key as appropriate. 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Add a predicate if we're filtering by user name 
    if (predicate) { 
     [fetchRequest setPredicate:predicate]; 
    } 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"]; 

    [fetchRequest release]; 
    [sortDescriptor release]; 
    [sortDescriptors release]; 

    return [fetchedResultsController autorelease]; 
} 

我在我UITableViewController創建自己的NSFetchedResultsController

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:[flickFetcher managedObjectContext]]; 

    [request setEntity:entity]; 

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:descriptor, nil]; 

    [request setSortDescriptors:sortDescriptors]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person = %@", person]; 

    [request setPredicate:predicate]; 

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[flickFetcher managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"]; 

    [request release]; 
    [descriptor release]; 
    [sortDescriptors release]; 

    NSError *error = nil; 
    [fetchedResultsController autorelease]; 
    [fetchedResultsController performFetch:&error]; 

我看不出區別。

回答

1

看不到任何事情。插入所有測試數據後,我想在上下文中插入保存。 (而不是周圍的下一次插入。)

一個評論:

photo.person = person; 
[person addPhotosObject:photo]; 

這是多餘的。如果您設置了另一個關係,那麼在覈心數據中與反向關係建模的關係將正確設置反向關係。所以,只需要其中的一條。

+0

感謝您的提示,我設法從上下文中檢索照片,這很奇怪,因爲我所做的只是創建自己的提取結果控制器,這與我之前使用的實用程序方法沒有什麼不同,但現在表格單元格不再是' t設置,cellForRowAtIndex永遠不會因爲某種原因被調用,我將更新代碼以顯示兩個fetchedResultsControllers。 – 8vius

+0

沒有cellForRowAtIndex:=沒有tableView.dataSource,或numberOfRowsInSection = 0 – bshirley

+0

是numberOfRowsInSection交易,這沒有把戲。注意我的fetchedResultsControllers有什麼不同?除了升序參數 – 8vius