2011-07-15 6 views
0

間違ったエンティティを返す...彼らは両方のカスタムクラスです...無視「実体」の指定を取得 - CoreDataをiOSの上で - そう、私は私のCoreDataモデルで2つのエンティティを持ってOK

最初ManagedObjectサブクラスがちょうどあり「ミリ」と呼ばれる一つの変数、...

@interface length : NSManagedObject { 
@private 
} 
@property (nonatomic, retain) NSNumber * mm; 

@end 

#import "length.h" 

...

@implementation length 
@dynamic mm; 

@end 

... [OK]を、他のManagedObjectサブクラスは、「タイムズ」と呼ばれ、多くの変数が、何を持っています奇妙な。

OKアプリ全体に対して1つのmanagedObjectContextがあります。

私はフェッチを行う3つのオブジェクトを持っています。それらはすべてフェッチ用のインスタンス変数を持ちますが、共有するのはmanagedObjectContext自体とそのpersistentStoreCoordinatorだけです。

問題は、1つのエンティティのフェッチがOTHERエンティティを含む配列を返すことです。しかし、なぜ?!?! ...

- (void)setUpLengths { 
    NSLog(@"Setting up focal lengths"); 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"length" inManagedObjectContext:MOC]; 
    [fetchRequest setEntity:entity]; 
    NSError *error = nil; 
    NSArray *myFetch = [MOC executeFetchRequest:fetchRequest error:&error]; 
    if(myFetch == nil) { 
     //handle error 
    } 

    length *thisNewLength = nil; 
    //thisNewLength = [[FocalLength alloc] initWithEntity:entity insertIntoManagedObjectContext:MOC]; (commented out this line to fix memory leak, but still had same problem when it was uncommented and released at the bottom of the method) 
    int x; 
    int l = [myFetch count]; 
    NSLog(@"lengths count: %i",l); 
    for(x = 0; x < l; x++) { 
     thisNewLength = [myFetch objectAtIndex:x]; 
     NSString *newEntry = [NSString stringWithFormat:@"%@",thisNewLength.mm]; 
     if([thisNewLength.mm intValue]) 
      [main_view_controller addLength:newEntry]; 
     NSLog(@"Added FL: %@",newEntry); 
    } 
    [fetchRequest release]; 
} 

と、ここで私は「タイムズ」(テーブル表示)のためにそれを行う方法です:ここ

見て、私は「長さ」クラスのための私のfetchRequestsを行う方法です

- (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [thisTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    Time *time = [fetchedResultsController objectAtIndexPath:indexPath]; 
    [self configureCell:cell withTime:time]; 

    //[CellIdentifier release]; 
    //fetchedResultsController = nil; 

    return cell; 
} 

- (NSFetchedResultsController *)fetchedResultsController { 
    if (fetchedResultsController != nil) { 
     return fetchedResultsController; 
    } 
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Time" inManagedObjectContext:MOC]; 
    [fetchRequest setEntity:entity]; 

    NSError *err; 
    if(![MOC countForFetchRequest:fetchRequest error:&err]) { 
     //handle error 
    } 
    int count = [MOC countForFetchRequest:fetchRequest error:&err]; 
    self.current_count = count; 
    if(count == NSNotFound) { 
     //Handle error 
    } 
    //NSLog(@"Times object count: %i", count); 

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

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:MOC sectionNameKeyPath:nil cacheName:@"Root"]; 

    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 

    //[fetchRequest release]; set to autorelease 
    [aFetchedResultsController release]; 
    [sortDescriptor release]; 
    [sortDescriptors release]; 

    return fetchedResultsController; 
} 



- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] withTime:anObject]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

- (void) tableView:(UITableView *)myTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Time *time = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
// selected_time = time; commented 7-11 

    selected_time = [self.fetchedResultsController objectAtIndexPath:indexPath]; //more direct 

    //fetchedResultsController = nil; 
    doneButtonItem.title = @"Use"; 
} 

- (void)tableView:(UITableView *)myTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the managed object for the given index path 
     Time *touched_time = [fetchedResultsController objectAtIndexPath:indexPath]; 
     NSNumber *slot = touched_time.slot; 
     //NSLog(@"SLot: %@",slot); 
     if(current_count < 6) { 
      UIAlertView *alert = [[UIAlertView alloc] 
            initWithTitle:@"Improper Deletion" 
            message:@"There must be at least five times." 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
      self.doneButtonItem.title = @"Done"; 
     } else if([slot intValue] != -1) { 
      UIAlertView *alert = [[UIAlertView alloc] 
            initWithTitle:@"Improper Deletion" 
            message:@"You can't delete a time currently assigned to a main slot." 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
      self.doneButtonItem.title = @"Done"; 
     } else { 
      [MOC deleteObject:touched_time]; 
      // Save the context. 
      NSError *error = nil; 
      if (![MOC save:&error]) { 
       /* 
       Replace this implementation with code to handle the error appropriately. 

       abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
       */ 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
      } 
      --current_count; 
      self.doneButtonItem.title = @"Done"; 
     } 
     //fetchedResultsController = nil; 
     //[slot release]; didn't alloc so why releasE? 

    } 
} 

..私は新しい追加するとき

は、なぜ、「長さを、」それは「時間」テーブル内の最初の項目として表示さありません!私は私の知恵の終わりでここにいるので、教えてください。私がCoreDataを理解したと思ったとき、それはこのような奇妙なことをして、私は私が狂っていなければならないと思っています。私はクリーンでクリーンなフォルダ、2つの異なるデバイスとシミュレータ、同じ問題を試してみました。フェッチリクエストが壊れているようです。どうすれば修正できますか?

+0

上記のインターフェイスファイルは、 'interface length:NSManagedObject'を持っています。あなたの実装は' FocalLength'用です - それは何ですか? – Rog

+0

申し訳ありませんが、それはタイプミスでした。実際のバージョンではそうではありません。 – CommaToast

+0

命名規則に従ってください。エンティティとクラスの名前は大文字で始まり、 'length'は' Length'になります。プロパティ名は意味があるはずですので、今から1年後にコードを読むときに、他の人やあなた自身に意味をなさせるものに 'mm'を展開してください。 – TechZen

答えて

1

のみ3つの原因は本当にあり:

(1)あなたはTimeエンティティリターンのフェッチLengthオブジェクトようTimesエンティティに不適切に設定されLengthクラスの実体を持っているが。

(2)LengthTimeのサブエンティティであり、Timeエンティティに対してフェッチが同様すべてLengthオブジェクトを返すようにデフォルトでは、戻りのサブエンティティをフェッチします。

(3)新しいLengthオブジェクトを作成するときは、実際にはTimeエンティティに対して行います。

最後は通常、例えば、同じエンティティのカスタムサブクラスとジェネリックNSManagedObjectsの使用を混合することによって引き起こされ:

あなたは1点で:いずれの場合で

NSManagedObject *newLength=[NSEntityDescription insertNewObjectForEntityForName:@"Time" inManagedObjectContext:self.managedObjectContext]; 

、私が最初だろう新しいLengthオブジェクトを作成するコードを見てください。上記の(1)または(2)の問題がデータモデルにない場合は、そこに存在します。

0

fetchedresultscontrollerのinitWithFetchRequestでcacheNameをnilに設定してみてください。私は、間違ったエンティティが返され、それを解決した同様の問題がありました。

関連する問題