在WatchKit中解析查询

SRM*_*RMR 1 iphone objective-c ios parse-platform watchkit

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

        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

这是我在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];
            });
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

但是我在这两条线上都有错误......

`PFQuery *query = [self queryForTable];`

`[self.tableView reloadData];`
Run Code Online (Sandbox Code Playgroud)

因为我无法做与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");
    }];
Run Code Online (Sandbox Code Playgroud)

-和-

苹果手机 AppDelegate.h

我怎么要求我PFQuery跑?

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    // What to put here?
}
Run Code Online (Sandbox Code Playgroud)

cno*_*oon 9

UITableView在WatchKit应用程序中没有这样的东西.相反,您必须使用WKInterfaceTable.在继续之前,我还建议您阅读WatchKit编程指南中的文档.它将使您更好地了解作为有抱负的Apple Watch开发人员可用的所有工具集.

WKInterfaceTable

一旦你了解了a的来龙去脉WKInterfaceTable,你就会很快发现为什么你的方法存在缺陷有两个原因.首先,你没有reloadData方法.WatchKit中的替代方案是setNumberOfRows(_:withRowTypes:).然后,您需要遍历每一行并进行配置.

WatchKit扩展中的PFQuery

您将遇到问题的第二个原因是由于您的使用PFQuery.

这是一个侧面建议,所以接受或离开它.我从这里的经验来讲,已经构建了一个非常大的基于页面的Watch App,它与iOS App进行了大量的通信.

我建议你停止PFQuery在你的WatchKit扩展中使用s.原因是使用您的Watch App的用户只会打开应用程序一两秒钟.一切都会发生得非常快.因此,在用户终止Watch App之前,很难保证网络呼叫的成功.这使事情变得更加困难,但事实就是如此.

相反,您希望PFQuery在iOS应用程序上运行调用,并通过以下调用将该信息返回到Watch Extension:

您还可以使用MMWormhole或类似方法替代缓存PFQuery共享应用程序组.下面是一个示例,说明如何让Watch Extension请求iOS应用程序运行PFQuery,将数据缓存在MMWormhole中,并在完成后通知Watch Extension.通过始终从缓存中读取数据,您可以获得一致的机制,即Watch Extension是否仍在运行以及关闭和重新打开.


Objective-C的

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!");
             }
        }];
    });
}
Run Code Online (Sandbox Code Playgroud)

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)});
}
Run Code Online (Sandbox Code Playgroud)

迅速

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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])
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于打破从缓存中读取数据的一致方式以及将网络请求(或PFQueries)卸载到iOS应用程序的复杂性.