私は以下の問題を抱えています:RestKitを使用してオブジェクトをREST APIから取得します。 RKデバッガ出力から見ることができるオブジェクトマッピングが機能します。しかし、後でフェッチ要求を実行すると、結果は空になります。私はNSManagedObjectsについて話しています。私は以下の設定をしています。FetchRequestはRestKit経由でフェッチされたNSManagedObjectsを返しません0.2
1:RestkitとCoredataスタックの初期化:
NSError *error;
NSURL *baseURL = [NSURL URLWithString:@"https://mfihost.us/gocoffee/api/V1/"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[objectManager.HTTPClient setDefaultHeader:@"Token" value:[NSString stringWithFormat:@"%@",[FBSDKAccessToken currentAccessToken].tokenString]];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
//[RKObjectManager setSharedManager:objectManager];
[FetchRequests registerFetchRequests:objectManager];
[Descriptors registerDescriptors:objectManager];
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add inmemory store with error: %@", error);
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
2:サーバからオブジェクトを取得するために呼び出し、その後、フェッチ要求を行う:
[[RKObjectManager sharedManager]
postObject:nil path:@"/gocoffee/api/V1/login/IOS"
parameters:nil
success: ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Objects have been saved in core data.");
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
// Shout* sh=[managedObjCtx insertNewObjectForEntityForName:@"Shout"];
// [email protected]"22223";
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Shout" inManagedObjectContext:managedObjCtx];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *result = [managedObjCtx executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Unable to execute fetch request.");
NSLog(@"%@, %@", error, error.localizedDescription);
} else {
NSLog(@"%@", result);
}
completionBlock();
}
failure: ^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];
フェッチ結果は、サーバが、空でありますオブジェクトを返し、これらのオブジェクトはRKEntityMappingsと対応する応答記述子を使用して適切にマッピングされます。紛らわしいことに、Shout * ....という2つの行のコメントを外すと(つまり、手動で管理対象オブジェクトをコンテキストに挿入すると)、このオブジェクトはフェッチ要求によってフェッチされます。したがって、フェッチ要求は正常に動作しているはずです。 私は今何年も問題を探しています。間違った文脈や何かを私が呼んでいることでしょうか?ところで、コアデータマルチスレッドデバッグが有効になり、エラーは表示されません。つまり、「AllThatIsLeftToUsIsHonor」エラーはありません。
上記の例からの対応する経路がある:
[objectManager.router.routeSet
addRoute:[RKRoute routeWithName:@"loginAndOrSignup"
pathPattern:@"login/IOS"
method:RKRequestMethodPOST]
];
記述子は、(例えば)のようになります。次のように
[objectManager addResponseDescriptor:
[RKResponseDescriptor responseDescriptorWithMapping:[shoutMapping inverseMapping]
method:RKRequestMethodPOST
pathPattern: @"login/IOS"
keyPath:@"response.incomingshoutapplications"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
]
];
シャウトマッピングがある:
RKEntityMapping *shoutMapping = [RKEntityMapping mappingForEntityForName:@"Shout" inManagedObjectStore:managedObjectStore];
shoutMapping.identificationAttributes = @[ @"id" ];
[shoutMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"inviterUserId" : @"inviterUserId",
@"eventType" : @"eventType",
@"eventTime" : @"eventTime",
@"locationLat" : @"locationLat",
@"locationLng" : @"locationLng",
@"radius" : @"radius",
@"targetGender" : @"targetGender",
@"state" : @"state",
@"buddyOnly" : @"buddyOnly",
@"timeCreated" : @"timeCreated"
}
];
"マネージャ "は上記のものではなく、managedObjectStoreはmanager.managedObjectStoreです [Descriptors registerDescriptors:objectManager]によって呼び出される別のメソッドで、すべてのマッピングと記述子が設定されます。 (コードの最初のブロックを参照してください)
に変更
は、私はすぐにこれを試してみるつもりです....マッピングコードとそれが返事が遅れて申し訳ありません最後 – Wain