nenosinc / iCloudDocumentSync

Integrate iCloud into iOS document projects with one-line code methods. Sync, upload, manage, and remove documents from iCloud quickly and easily. Helps to make iCloud "just work" for developers too.
Other
1.22k stars 178 forks source link

Optionally Suspend Updates #121

Closed hassanvfx closed 5 years ago

hassanvfx commented 6 years ago

Hey guys!

We recently ran into an issue where the updates from iCloud where causing a CollectionView scroll to be sluggish. We tracked down to the callback caused after saving the document.

It seems like the problem is that after saving then the device gets an "echo" with the updates just pushed. While this redundancy might be fine for syncing having the ability to delay this update is important in order to keep the UI fluent. This is especially true given the receiveUpdate callback triggers updateFiles that is resource intensive.

As a solution, we wrapped the update events into a single task operation queue and provided an accessor to suspend it when needed. The operation always gets enqueued and then processed at a later time when the suspension is released.

- (void)recievedUpdate:(NSNotification *)notification {

    __weak __typeof(self) wself=self;
    [self.updatesQueue addOperationWithBlock:^{
        // Log file update
        if (wself.verboseLogging == YES) NSLog(@"[iCloud] An update has been pushed from iCloud with NSMetadataQuery");

        // Get the updated files
        [wself updateFiles];
    }];

}

Please take a look, it may come handy for other users!

Sam-Spencer commented 5 years ago

Looks great! Thank you so much!