在iOS 8中获取parse.com用户数据时的例外情况扩展程序

问题描述:

我正在尝试获取PFUser的PFObjects列表以显示在iOS 8 Today窗口小部件中。在iOS 8中获取parse.com用户数据时的例外情况扩展程序

在此之后blog post通过解析,我已经启用了相同的应用程序组和钥匙扣共享在Xcode中我主要的应用程序和扩展两者。

我也能在我的主要应用程序的AppDelegate和我的今天,扩展的viewDidLoad如下:

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

widgetPerformUpdateWithCompletionHandler,我构建和执行我的查询:

- (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); 
     } 
     }]; 
    } 
} 

第一次加载似乎工作正常 - 我能够得到我的PFObjects列表。但是,每当分机重新加载第二次时,viewDidLoad中的enableDataSharingWithApplicationGroupIdentifier呼叫将抛出以下例外:enableDataSharingWithApplicationGroupIdentifier:containingApplication:' must be called before 'setApplicationId:clientKey''

我可以通过复制刷卡通知中心的“通知”选项卡,然后刷卡回这个重载,这将导致viewDidLoad中被再次调用。

我反复检查调用方法的顺序是正确的,甚至与秩序修修补补,但我仍然得到崩溃。

任何想法?提前致谢!

试试这个

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

确保解析初始化一次内延伸减轻了碰撞。但是,当我登录或退出主应用程序时,[PFUser currentUser]的状态现在保持不变。 –

+0

你能提供你的代码,你在哪里使用PFUser? – Sandr

+0

我正在访问[PFUser currentUser]上面的初始化步骤,在我的扩展的viewDidLoad。 –