Permalink
Browse files

Moved the URL iteration in the ubiquity monitor to a background queue…

…. It could sometimes take a while, and block the main queue.
  • Loading branch information...
1 parent 6384fe3 commit 8c07881a1651d7634c4a5c6032f6bc50cf0d02ed @drewmccormack committed Mar 9, 2013
Showing with 52 additions and 38 deletions.
  1. +52 −38 TICoreDataSync/06 External Sources/TIUbiquityMonitor.m
@@ -117,47 +117,61 @@ - (void)update:(NSNotification *)notif
{
[metadataQuery disableUpdates];
- NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
- NSUInteger count = [metadataQuery resultCount];
- long long toDownload = 0, toUpload = 0;
- for ( NSUInteger i = 0; i < count; i++ ) {
- NSURL *url = [metadataQuery valueOfAttribute:NSMetadataItemURLKey forResultAtIndex:i];
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
+ NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
+ __block BOOL downloadErrorArose = NO, uploadErrorArose = NO;
+ NSError *downloadError = nil, *uploadError = nil;
- NSNumber *percentDownloaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemPercentDownloadedKey forResultAtIndex:i];
- NSNumber *percentUploaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemPercentUploadedKey forResultAtIndex:i];;
- NSNumber *downloaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemIsDownloadedKey forResultAtIndex:i];
- NSNumber *uploaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemIsUploadedKey forResultAtIndex:i];
- NSNumber *fileSizeNumber = [metadataQuery valueOfAttribute:NSMetadataItemFSSizeKey forResultAtIndex:i];
-
- unsigned long long fileSize = fileSizeNumber ? fileSizeNumber.unsignedLongLongValue : 0;
- if ( downloaded && !downloaded.boolValue ) {
- double percentage = percentDownloaded ? percentDownloaded.doubleValue : 0.0;
- long long fileDownloadSize = (1.0 - percentage / 100.0) * fileSize;
- toDownload += fileDownloadSize;
-
- // Start download
- NSError *error = nil;
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) NSLog(@"Failed to initiate download with error: %@", error);
+ NSUInteger count = [metadataQuery resultCount];
+ long long toDownload = 0, toUpload = 0;
+ for ( NSUInteger i = 0; i < count; i++ ) {
+ @autoreleasepool {
+ NSURL *url = [metadataQuery valueOfAttribute:NSMetadataItemURLKey forResultAtIndex:i];
+
+ NSNumber *percentDownloaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemPercentDownloadedKey forResultAtIndex:i];
+ NSNumber *percentUploaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemPercentUploadedKey forResultAtIndex:i];
+ NSNumber *downloaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemIsDownloadedKey forResultAtIndex:i];
+ NSNumber *uploaded = [metadataQuery valueOfAttribute:NSMetadataUbiquitousItemIsUploadedKey forResultAtIndex:i];
+ NSNumber *fileSizeNumber = [metadataQuery valueOfAttribute:NSMetadataItemFSSizeKey forResultAtIndex:i];
+
+ unsigned long long fileSize = fileSizeNumber ? fileSizeNumber.unsignedLongLongValue : 0;
+ if ( downloaded && !downloaded.boolValue ) {
+ double percentage = percentDownloaded ? percentDownloaded.doubleValue : 0.0;
+ long long fileDownloadSize = (1.0 - percentage / 100.0) * fileSize;
+ toDownload += fileDownloadSize;
+
+ // Start download
+ if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&downloadError] ) downloadErrorArose = YES;
+ }
+ else if ( uploaded && !uploaded.boolValue ) {
+ double percentage = percentUploaded ? percentUploaded.doubleValue : 0.0;
+ long long fileDownloadSize = (1.0 - percentage / 100.0) * fileSize;
+ toUpload += fileDownloadSize;
+
+ // Force upload
+ if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&uploadError] ) uploadErrorArose = YES;
+ }
+ }
}
- else if ( uploaded && !uploaded.boolValue ) {
- double percentage = percentUploaded ? percentUploaded.doubleValue : 0.0;
- long long fileDownloadSize = (1.0 - percentage / 100.0) * fileSize;
- toUpload += fileDownloadSize;
+
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if ( downloadErrorArose ) {
+ NSLog(@"Failed to initiate download(s) with last error: %@", downloadError);
+ }
+ if ( uploadErrorArose ) {
+ NSLog(@"Failed to initiate download(s) with last error: %@", uploadError);
+ }
- // Force upload
- NSError *error = nil;
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) NSLog(@"Failed to initiate upload with error: %@", error);
- }
- }
-
- ubiquitousBytesToDownload = toDownload;
- ubiquitousBytesToUpload = toUpload;
-
- if ( progressCallbackBlock ) progressCallbackBlock(ubiquitousBytesToDownload, ubiquitousBytesToUpload);
-
- [metadataQuery enableUpdates];
-
- [self scheduleRefresh];
+ ubiquitousBytesToDownload = toDownload;
+ ubiquitousBytesToUpload = toUpload;
+
+ if ( progressCallbackBlock ) progressCallbackBlock(ubiquitousBytesToDownload, ubiquitousBytesToUpload);
+
+ [metadataQuery enableUpdates];
+
+ [self scheduleRefresh];
+ });
+ });
}
@end

0 comments on commit 8c07881

Please sign in to comment.