4

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!

Ken Toh
  • 3,721
  • 1
  • 24
  • 30

1 Answers1

1

Try this

if(![Parse isLocalDatastoreEnabled]) {
    [Parse enableLocalDatastore];
    [Parse enableDataSharingWithApplicationGroupIdentifier:@"group.com.me.myapp" containingApplication:@"com.me.myapp"];
    [Parse setApplicationId:@"myAppId" clientKey:@"myClientId"];
}
Sandr
  • 189
  • 9
  • Making sure Parse initializes only once within the extension mitigates the crash. However, the state of [PFUser currentUser] now remains unchanged when I log in or out of the main application. – Ken Toh Jun 27 '15 at 09:27
  • Could you provide your code, where you are using PFUser? – Sandr Jun 28 '15 at 10:03
  • I'm accessing [PFUser currentUser] right after the initialization steps above, in the viewDidLoad of my extension. – Ken Toh Jun 28 '15 at 16:01
  • Are you getting PFUser from LocalDatastore in your extension? Have you changing pinned PFUser on logout\login in your main app? – Sandr Jun 29 '15 at 21:39
  • No, I am not retrieving the PFUser from my local datastore. According to the docs at https://parse.com/docs/ios/guide#extensions-shared-local-data, the current PFUser should be persisted across apps so I was expecting the syncing to be seamless, without any form of manual pinning or retrieving. Neither have i changed the pinned PFUser upon logging in and out of my app. – Ken Toh Jun 29 '15 at 23:41
  • You Sir, just made my day, this issue was driving me crazy! – sachadso Dec 04 '15 at 15:13
  • Wowowowo Thanks man! I was suffering a lot with this stupid bug. – Perjan Duro Jan 07 '16 at 14:16