Exception when fetching parse.com user data in iOS 8 Today Extension

387 views Asked by At

I am trying to fetch a list of PFObjects of a PFUser to display in the iOS 8 Today Widget.

Following this blog post by Parse, I've enabled the same App Groups and Keychain Sharing in both my main app and extension in Xcode.

I've also enabled the following in the AppDelegate of my main app and the viewDidLoad of my Today Extension:

[Parse enableLocalDatastore];
[Parse enableDataSharingWithApplicationGroupIdentifier:@"group.com.me.myapp" containingApplication:@"com.me.myapp"];
[Parse setApplicationId:@"myAppId" clientKey:@"myClientId"];

In widgetPerformUpdateWithCompletionHandler, I constructed and performed my query:

- (void) widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
      PFQuery *query = [PFQuery queryWithClassName:@"Note"];
      [query whereKey:@"User" equalTo:[PFUser currentUser]];

      [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         if (!error)
         {
            // check for difference between current and new data
            if([self hasNewData:objects]) {
                // fresh data
                notes = objects;
                [self.tableView reloadData];
                [self updatePreferredContentSize];
                completionHandler(NCUpdateResultNewData);
            } else {
                // Data is the same
                completionHandler(NCUpdateResultNoData);
            }
         } else {
            // Failed
            completionHandler(NCUpdateResultFailed);
         }
      }];
    }
}

The first load seems to work fine - I'm able to get my list of PFObjects. However, whenever the extension reloads a second time, the following exception: enableDataSharingWithApplicationGroupIdentifier:containingApplication:' must be called before 'setApplicationId:clientKey'' is thrown at the enableDataSharingWithApplicationGroupIdentifier call in viewDidLoad.

I can replicate this reload by swiping the Notification Center to the "Notifications" tab and swiping it back, which would cause viewDidLoad to be invoked again.

I've double-checked that the order of calling the methods are right, and even tinkered with the order but am still getting the crash.

Any ideas? Thanks in advance!

1

There are 1 answers

7
Sandr On

Try this

if(![Parse isLocalDatastoreEnabled]) {
    [Parse enableLocalDatastore];
    [Parse enableDataSharingWithApplicationGroupIdentifier:@"group.com.me.myapp" containingApplication:@"com.me.myapp"];
    [Parse setApplicationId:@"myAppId" clientKey:@"myClientId"];
}