Permalink
Browse files

Reworked the ubiquity monitor again. It was stalling the main queue f…

…or too long. Now move the download initiations to the background, and it seems to work better.
  • Loading branch information...
1 parent a3943dc commit 5eadbb8e5f5b523391b5dfe7eae1adb2216024fc @drewmccormack committed Mar 16, 2013
Showing with 12 additions and 22 deletions.
  1. +12 −22 TICoreDataSync/06 External Sources/TIUbiquityMonitor.m
@@ -118,8 +118,6 @@ - (void)update:(NSNotification *)notif
[metadataQuery disableUpdates];
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
- BOOL downloadErrorArose = NO, uploadErrorArose = NO;
- NSError *downloadError = nil, *uploadError = nil;
NSUInteger count = [metadataQuery resultCount];
long long toDownload = 0, toUpload = 0;
@@ -139,11 +137,12 @@ - (void)update:(NSNotification *)notif
toDownload += fileDownloadSize;
// Start download
- NSError *error;
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) {
- if ( downloadErrorArose ) [downloadError release]; // Release old error
- downloadErrorArose = YES;
- downloadError = [error retain];
+ if ( initiateTransfers && percentage < 1.e-6 ) {
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
+ NSError *error;
+ BOOL startedDownload = [fileManager startDownloadingUbiquitousItemAtURL:url error:&error];
+ if ( !startedDownload ) NSLog(@"Error starting download: %@", error);
+ });
}
}
else if ( uploaded && !uploaded.boolValue ) {
@@ -152,26 +151,17 @@ - (void)update:(NSNotification *)notif
toUpload += fileDownloadSize;
// Force upload
- NSError *error;
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) {
- if ( uploadErrorArose ) [uploadError release];
- uploadErrorArose = YES;
- uploadError = [error retain];
+ if ( initiateTransfers && percentage < 1.e-6 ) {
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
+ NSError *error;
+ BOOL uploadStarted = [fileManager startDownloadingUbiquitousItemAtURL:url error:&error];
+ if ( !uploadStarted ) NSLog(@"Error starting upload: %@", error);
+ });
}
}
}
}
- // Log last error
- if ( downloadErrorArose ) {
- NSLog(@"Failed to initiate download(s) with last error: %@", downloadError);
- }
- if ( uploadErrorArose ) {
- NSLog(@"Failed to initiate download(s) with last error: %@", uploadError);
- }
- [downloadError release];
- [uploadError release];
-
// Update and callback
ubiquitousBytesToDownload = toDownload;
ubiquitousBytesToUpload = toUpload;

0 comments on commit 5eadbb8

Please sign in to comment.