I have an app which gets sensor data and loads that into my dashboard using CloudKit. It does it even when in background if only once in a great while. If the AppleWatch is open and the iphone app is opened and updates then the AppleWatch updates at that time from the cloud. I know it is the cloud and not bluetooth because I have bluetooth turned off on my phone.
I want to know why when the watch is in the background how I get it to update its cloud data.
Here is my code:
On the iphone
CKRecordID *recordID = [[CKRecordID alloc]initWithRecordName:@"myPoolString"];
[[CKContainer containerWithIdentifier:container].privateCloudDatabase fetchRecordWithID:recordID
completionHandler:^(CKRecord *record, NSError *error) {
record = [[CKRecord alloc] initWithRecordType: @"MyPoolInfo"];
[record setObject:time forKey:@"time"];
[record setObject: self.myTemp forKey: @"temperature"];
[record setObject: self.myDate forKey: @"date"];
[record setObject: self.myLevel forKey: @"level"];
[record setObject: self.myTrip forKey: @"trip"];
[record setObject: self.myBattery forKey: @"battery"];
NSArray *myFields = [NSArray arrayWithObjects:record,nil];
//NSArray *recordsToSave = [NSArray arrayWithObjects:self.myTemp,self.myDate,self.myLevel,self.myTrip,self.myBattery, nil];
CKModifyRecordsOperation *modifyRecords= [[CKModifyRecordsOperation alloc]
initWithRecordsToSave:myFields recordIDsToDelete:nil];
//[modifyRecords setValue:@"one" forKey:@"temperature"];
modifyRecords.savePolicy=CKRecordSaveAllKeys;
modifyRecords.qualityOfService=NSQualityOfServiceUserInitiated;
modifyRecords.modifyRecordsCompletionBlock=
^(NSArray * savedRecords, NSArray * deletedRecordIDs, NSError * operationError){
// the completion block code here
NSLog(@"savedRecords:%@",savedRecords);
};
[[CKContainer containerWithIdentifier:container].privateCloudDatabase addOperation:modifyRecords];
and on my watch extension interface controller
else if (applicationContext == nil){
NSLog(@"UseWifi");
//do this
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"MyPoolInfo" predicate:predicate];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO];
query.sortDescriptors = @[sortDescriptor];
CKQueryOperation *queryOp = [[CKQueryOperation alloc]initWithQuery:query];
queryOp.resultsLimit = 1;
[[CKContainer containerWithIdentifier:container].privateCloudDatabase performQuery:query
inZoneWithID:nil
completionHandler:^(NSArray *results, NSError *error) {
if (error) {
NSLog(@"An error occurred: %@",
[error localizedDescription]);
}
else {}}
Would I put this in the extensionDelegate -(void) handleBackgroundTasks / refreshTask?
I would really appreciate some help.
question from:
https://stackoverflow.com/questions/65907937/applewatch-not-always-loading-latest-cloudkit-stored-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…