我已经开始使用StackMob将Facebook整合到我的应用中。 我一直在尝试使用StackMob的离线同步,但它似乎已经坏了。
在didFinishLaunchingWithOptions中:
SM_CACHE_ENABLED = YES;
SMClient *client = [[SMClient alloc] initWithAPIVersion:@"0" publicKey:@"cc5f57c8-4a5d-424d-a3e1-0594c675cad8"];
SMCoreDataStore *coreDataStore = [client coreDataStoreWithManagedObjectModel:self.managedObjectModel];
coreDataStore.cachePolicy = SMCachePolicyTryCacheOnly;
_managedObjectContext = [coreDataStore contextForCurrentThread];
当用户使用Facebook登录时:
[[SMClient defaultClient] loginWithFacebookToken:FBSession.activeSession.accessTokenData.accessToken createUserIfNeeded:YES usernameForCreate:nil onSuccess:^(NSDictionary *result) {
NSLog(@"Logged in with StackMob!");
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
[self.managedObjectContext executeFetchRequest:fetchRequest returnManagedObjectIDs:NO onSuccess:^(NSArray *results) {
NSLog(@"users: %@",results);
} onFailure:^(NSError *error) {
NSLog(@"error: %@",error);
}];
} onFailure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
问题是fetchRequest的结果是一个空数组,尽管用户已成功创建。 当我将缓存策略更改为SMCachePolicyTryCacheElseNetwork时,fetchRequest的结果不是空数组。
为什么用户没有保存在缓存中?另外,如何告诉StackMob只在缓存中保存一些对象?
非常感谢你!
答案 0 :(得分:1)
这听起来像是一个可以报告它的错误here。您可以解决此问题,因此您不必使用每个请求缓存策略全局设置SMCachePolicyTryCacheElseNetwork。将您的获取请求更改为:
SMRequestOptions *options = [SMRequestOptions options];
options.cachePolicy = SMCachePolicyTryCacheElseNetwork;
[self.managedObjectContext executeFetchRequest:fetchRequest
returnManagedObjectIDs:NO
successCallbackQueue:dispatch_get_main_queue()
failureCallbackQueue:dispatch_get_main_queue()
options:options
onSuccess:(SMResultsSuccessBlock)successBlock
onFailure:(SMFailureBlock)failureBlock];
请注意,每个请求缓存策略都需要最新的2.1 SDK。