0

I have started to integrate Facebook in my app using StackMob. I've been trying to use StackMob's offline sync, but it just seems broken.

In 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];

When a user logs in with 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);
                            }];

The problem is that the fetchRequest's result is an empty array, although the user has been created successfully. When I change my cache policy to SMCachePolicyTryCacheElseNetwork, the fetchRequest's result is not an empty array.

Why isn't the user saved in the cache?? Also, how can I tell StackMob to save some objects in the cache only?

Thank you so much!

Noam Solovechick
  • 1,127
  • 2
  • 15
  • 29

1 Answers1

1

It sounds like a bug you can report it here. You could work around this so you don't have to globally set SMCachePolicyTryCacheElseNetwork, by using a per request cache policy. Change your fetch request to:

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];

Note that per request cache policies require the latest 2.1 SDK.

combinatorial
  • 9,132
  • 4
  • 40
  • 58
  • I was hoping that it isn't a bug. I've decided to use the StackMob's Rest API instead because it seems more stable and then I'll have full control over the cache. Thank you! – Noam Solovechick Aug 06 '13 at 00:00