Hello,
I have an iOS app and a companion watchOS app. Users record a workout on Apple Watch, the data for which is then transferred using both Watch Connectivity and Core Data + CloudKit (NSPersistentCloudKitContainer)
to their iPhone, where it is processed and displayed.
As users are recording the workout on their Apple Watch, when they finish and the transfer begins, their iPhone is often not reachable to immediately send the data using Watch Connectivity and they have no network connection (cellular or Wi-Fi).
With Watch Connectivity I use transferFile
from WCSession
, which queues the file for transfer. With Core Data + Cloudkit I save the data and the export is queued.
An undetermined amount of time may pass until the user returns to their iPhone or connects to Wi-Fi and most of the time neither of the transfer methods actually transfers the data until the user opens the watchOS app into the foreground, at which point the transfer happens immediately for both methods.
I've tried a number of things already, without success, such as:
- Using
sendMessage
fromWCSession
to send an immediate message to the watchOS app when the iOS app returns to the foreground to try and wake the watchOS app up so it can complete the data transfer. - On the watchOS app, after attempting to transfer the data, using
downloadTask
fromURLSession
to queue a background task to download something, in the hope that it would wake the watchOS app when network connectivity was restored and enable it to complete the data transfer. - On the watchOS app, instead of saving the data using
NSPersistentCloudKitContainer,
usingCKRecord
andCKDatabase
directly to save the data usinguserInitiated
as the quality of service, in the hope that it would be exported once network connectivity was restored.
Is there a way to trigger the watchOS app to transfer the data using Watch Connectivity or Core Data + CloudKit in the background when reachabillity or network connectivity is restored, even if the app may have been suspended by watchOS?
Many Thanks,
Alex
There isn't, and I see that an as-designed behavior. When a file is queued for a Watch Connectivity transfer, or a change is queued for a Core Data + CloudKit export, it is up to the system to decide when the operation should happen. That is for optimizing the overall system performance, and there is no way for developers to change the behavior. This is documented in the following API reference and technote respectively:
The system attempts to send files as quickly as possible but may throttle delivery speeds to accommodate performance and power concerns.
The goal of implementing such a mechanism is to balance the use of system resources and achieve the best overall user experience on the devices. There is no API for apps to configure the timing for the synchronization.
So even you have your watchOS app run in the background (for a period of time) using something like background workout processing or HealthKit background delivery, detect the reachability change, and trigger a transferFile
or a Core Data save, the system may choose to not transfer or synchronize the data immediately.
Having said that, if you still see the behavior an issue for your app, I'd suggest that you file a feedback report with your concrete use case for the folks to consider.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.