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

人走茶凉 提交于 2019-12-10 08:54:21

问题


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:


Try this

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


来源:https://stackoverflow.com/questions/30846723/exception-when-fetching-parse-com-user-data-in-ios-8-today-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!