0

AFIncrementalStore を使用して、非常に単純な NSIncrementalStore の例を設定しています。

アイデアは、AppDelegate で NSManagedObjectContext をセットアップし (Apple が提供する通常のテンプレートを使用し、私の IncrementalStore に変更を加えて)、述語またはソート記述子なしでフェッチを実行し、NSLog を 1 つのフェッチされたエンティティ オブジェクトにすることです。

エンティティ属性を要求するまで、すべてがうまく機能します。次のメッセージでクラッシュします。

2013-07-22 16:34:46.544 AgendaWithAFIncrementalStore[82315:c07] -[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060
2013-07-22 16:34:46.545 AgendaWithAFIncrementalStore[82315:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060'

私の xcdatamodeld は正しく設定されています。NSManagedObject クラスが生成され、デリゲートにインポートされます。NSLog の前にブレークポイントを設定すると、フェッチされたオブジェクト ID が表示されます。Web サービスは正しいデータを返してくれます。

私のAppDelegateコード:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ... 
    [self.window makeKeyAndVisible];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteFetchHappened:) name:AFIncrementalStoreContextDidFetchRemoteValues object:self.managedObjectContext];

    NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Agenda" inManagedObjectContext:self.managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.entity = entityDescription;
    fetchRequest.predicate = nil;
    NSError *error;

    [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    return YES;
}

// Handle the notification posted when the webservice returns objects
- (void)remoteFetchHappened:(NSNotification *)aNotification
{
    NSArray *fetchResult = [[aNotification userInfo] objectForKey:@"AFIncrementalStoreFetchedObjectIDs"];
    Agenda *agenda = (Agenda *)[fetchResult lastObject];

    // THIS IS WHERE IT BREAKS...
    NSLog(@"Agenda: %@", agenda.eventoId);
}

このコードを作成して、私が求めている属性を返す方法についてのアイデアはありますか?

4

1 に答える 1

0

AFNetworking は、管理対象オブジェクト ID、つまり のインスタンスを提供していますNSManagedObjectID。その上で管理オブジェクトのプロパティ値を検索することはできません。最初に ID の管理オブジェクトを取得する必要があります。これ_NSObjectID_id_0がエラー メッセージの意味です。あなたは に乗ろうとしていますが、 eventoIdそれが何であるかわかりませんNSManagedObjectID

管理オブジェクト コンテキストで検索することにより、管理オブジェクトを取得します。何かのようなもの

NSError *error = nil;
NSManagedObject *myObject = [context existingObjectWithID:objectID error:error];
if (myObject != nil) {
    // look up attribute values on myObject
}
于 2013-07-22T20:37:10.710 に答える