我在Parse
应用中进行了iPhone
查询,但是我在Parse
应用中尝试执行相同的Watch
查询时出现错误
这是我的iPhone应用中的查询:
- (void)viewDidLoad {
// GMT Date from Phone
NSDate *gmtNow = [NSDate date];
NSLog(@"GMT Now: %@", gmtNow);
// Query Parse
PFQuery *query = [self queryForTable];
[query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localMatchup = [@[] mutableCopy];
for (PFObject *object in objects) {
// Add objects to local Arrays
[localMatchup addObject:[object objectForKey:@"matchup"]];
// App Group
NSString *container = @"group.com.me.off";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
NSLog(@"Default Matchup: %@", savedMatchup);
savedMatchup = matchupArray;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}];
}
这是我在WatchKit应用程序中尝试的查询...
- (void)awakeWithContext:(id)context {
// GMT Date from Phone
NSDate *gmtNow = [NSDate date];
NSLog(@"GMT Now: %@", gmtNow);
// Query Parse
PFQuery *query = [self queryForTable];
[query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localMatchup = [@[] mutableCopy];
for (PFObject *object in objects) {
// Add objects to local Arrays
[localMatchup addObject:[object objectForKey:@"matchup"]];
// App Group
NSString *container = @"group.com.me.off";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
NSLog(@"Default Matchup: %@", savedMatchup);
savedMatchup = self.matchupArray;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}];
}
但我在这两行上都有错误......
`PFQuery *query = [self queryForTable];`
`[self.tableView reloadData];`
因为我无法执行与table
猜测相关的完全相同的代码,但我不知道该将其更改为什么。
编辑:每个@cnoon回答添加代码
WatchKit
InterfaceController.m
:
我如何要求我的查询在此处运行? - (void)awakeWithContext:(id)context { [super awakeWithContext:context];
[WKInterfaceController openParentApplication:nil reply:^(NSDictionary *replyInfo, NSError *error) {
// What to put here?
NSLog(@"Open Parent Application");
}];
- 和 -
iPhone AppDelegate.h
我如何要求PFQuery
投放?
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
// What to put here?
}
答案 0 :(得分:8)
WatchKit应用程序中没有UITableView
这样的东西。相反,您必须使用WKInterfaceTable。在继续之前,我还建议您仔细阅读WatchKit Programming Guide中的文档。它将使您更好地了解作为有抱负的Apple Watch开发人员可以使用的所有工具集。
一旦你知道了WKInterfaceTable
的细节,你就会很快看到为什么你的方法存在缺陷有两个原因。首先,您没有reloadData
方法。 WatchKit中的替代方案是setNumberOfRows(_:withRowTypes:)
。然后,您需要遍历每一行并进行配置。
您遇到问题的第二个原因是您使用了PFQuery
。
这是一个侧面建议,所以接受或离开它。我从这里的经验谈起,已经构建了一个非常大的基于页面的Watch App,它与iOS App进行了大量的通信。
我建议你停止在WatchKit Extension中制作PFQuery
。原因是使用您的Watch App的用户只会打开应用程序一两秒钟。一切都会发生得非常快。因此,在用户终止Watch App之前,很难保证网络呼叫的成功。这使得事情变得更加困难,但事实就是如此。
相反,您希望在iOS应用上运行PFQuery
次呼叫,并通过以下呼叫将该信息返回到Watch Extension:
WKInterfaceController
- openParentApplication(_:reply:) UIApplicationDelegate
- handleWatchKitExtensionRequest(_:reply:) 您也可以使用shared app group或类似的方法将PFQuery
缓存到MMWormhole。下面是一个示例,说明如何让Watch Extension请求iOS应用程序运行PFQuery,将数据缓存在MMWormhole中,并在完成后通知Watch Extension。通过始终从缓存中读取数据,无论Watch Extension是否仍在运行以及关闭和重新打开,您都有一个一致的机制。
<强> InterfaceController.m 强>
- (void)willActivate {
[super willActivate];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[WKInterfaceController
openParentApplication:@{@"pfquery_request": @"dumm_val"}
reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"User Info: %@", replyInfo);
NSLog(@"Error: %@", error);
if ([replyInfo[@"success"] boolValue]) {
NSLog(@"Read data from Wormhole and update interface!");
}
}];
});
}
<强> AppDelegate.m 强>
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
if (userInfo[@"pfquery_request"]) {
NSLog(@"Starting PFQuery"); // won't print out to console since you're running the watch extension
// 1. Run the PFQuery
// 2. Write the data into MMWormhole (done in PFQuery completion block)
// 3. Send the reply back to the extension as success (done in PFQuery completion block)
reply(@{@"success": @(YES)});
}
reply(@{@"success": @(NO)});
}
<强> InterfaceController.swift 强>
override func willActivate() {
super.willActivate()
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Float(NSEC_PER_SEC))), dispatch_get_main_queue()) {
WKInterfaceController.openParentApplication(["pfquery_request": "dummy_val"]) { userInfo, error in
println("User Info: \(userInfo)")
println("Error: \(error)")
if let success = (userInfo as? [String: AnyObject])?["success"] as? NSNumber {
if success.boolValue == true {
println("Read data from Wormhole and update interface!")
}
}
}
return
}
}
<强> AppDelegate.swift 强>
func application(
application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
if let pfqueryRequest: AnyObject = (userInfo as? [String: AnyObject])?["pfquery_request"] {
println("Starting PFQuery") // won't print out to console since you're running the watch extension
// 1. Run the PFQuery
// 2. Write the data into MMWormhole (done in PFQuery completion block)
// 3. Send the reply back to the extension as success (done in PFQuery completion block)
reply(["success": true])
}
reply(["success": false])
}
希望这有助于打破从缓存中读取数据的一致方式以及将网络请求(或PFQueries)卸载到iOS应用程序的复杂性。