0

私の質問は簡単です。モデルとコントローラの通信方法あなたが提供されているコードからNSManagedObjectNSManagedObjectからuiviewController(MVCパターン)の呼び出しメソッド

(NSArray *) returnAllAlbums 
{ 

NSEntityDescription *entitydescription = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:managedObjContextAlbum]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entitydescription]; 

NSError *error; 
NSArray *arrayListAlbums = [managedObjContextAlbum executeFetchRequest:request error:&error]; 

return arrayListAlbums; 
} 
+0

を – neo6583

+0

エラーは何ですか? –

答えて

1

にコントローラ

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:manageContext]; 
album = [[Album alloc] initWithEntity:entity insertIntoManagedObjectContext:manageContext]; 
NSArray *arrayListAlbums = [album returnAllAlbums]; 

メソッドに

コード、あなたのNSManagedObjectサブクラス内の便利なメソッドを追加して、その後、あなたはにNSManagedObjectのインスタンスを作成しますその便利なメソッドを呼び出します。これは良いデザインではありません。

フェッチメソッドを呼び出すために空のデータオブジェクトを作成しています。おそらく、あなたは、クラスメソッドを持つべき「どこか」それは次のようになります。

あなたは、空のデータオブジェクト作成しなくてもアクセスすることができます
+ (NSArray *)returnAllAlbums:(NSManagedObjectContext*)moc 
{ 
    NSEntityDescription *entitydescription = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:moc]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entitydescription]; 

    NSError *error; 
    NSArray *arrayListAlbums = [managedObjContextAlbum executeFetchRequest:request error:&error]; 
    if (arrayListAlbums == nil) { 
     NSLog(@"Failed to fetch: %@\n%@", [error localizedDescription], [error userInfo]); 
    } 

    return arrayListAlbums; 
} 

:私はViewControllerを以来returnAllAlbumsを呼び出すときに私はエラーが発生している

NSArray *arrayListAlbums = [MyClassWithFetchMethods returnAllAlbums:myLocalContextReference]; 
関連する問題