Permalink
Browse files

Added a method to the file manager based app manager that downloads a…

…ll iCloud data, and continuously calls back with progress updates.
  • Loading branch information...
1 parent a83a1cc commit 62ab2830d1d0dcd663f26bf3be31e6812749d243 @drewmccormack committed Feb 12, 2013
@@ -24,6 +24,8 @@ extern NSString * const TICDSApplicationSyncManagerDidRefreshCloudTransferProgre
NSURL *_applicationContainingDirectoryLocation;
NSMetadataQuery *_cloudMetadataQuery;
NSMetadataQuery *_transferProgressMetadataQuery;
+ NSMetadataQuery *_downloadMetadataQuery;
+ void (^_downloadProgressBlock)(BOOL complete, long long remainingBytes, BOOL *stop);
}
@property (readonly) unsigned long long cloudBytesToUpload, cloudBytesToDownload;
@@ -33,6 +35,9 @@ extern NSString * const TICDSApplicationSyncManagerDidRefreshCloudTransferProgre
/** Start a new metadata query to asynchronously download cloud files not yet local. */
- (void)refreshCloudMetadataQuery;
+/** Use a metadata query to download all data from the cloud. */
+- (void)downloadAllCloudData:(void(^)(BOOL complete, long long remainingBytes, BOOL *stop))block;
+
/** Start a metadata query to scan iCloud files, and determine how much data still needs to be uploaded and downloaded.
Access the results using the provided properties. Use the notification to determine when the asynchronous
calculation is finished. */
@@ -176,6 +176,65 @@ - (void)initiateDownloadsForURLs:(NSArray *)urls
[fm release];
}
+- (void)downloadAllCloudData:(void(^)(BOOL complete, long long remainingBytes, BOOL *stop))block
+{
+ if ( _downloadMetadataQuery ) return;
+ _downloadProgressBlock = [block copy];
+ _downloadMetadataQuery = [[NSMetadataQuery alloc] init];
+ _downloadMetadataQuery.searchScopes = [NSArray arrayWithObject:NSMetadataQueryUbiquitousDataScope];
+ _downloadMetadataQuery.predicate = [NSPredicate predicateWithFormat:@"%K like '*'", NSMetadataItemFSNameKey];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishedGatheringCloudDownloadURLs:) name:NSMetadataQueryDidFinishGatheringNotification object:_downloadMetadataQuery];
+ [_downloadMetadataQuery startQuery];
+}
+
+- (void)finishedGatheringCloudDownloadURLs:(NSNotification *)notif
+{
+ [_downloadMetadataQuery disableUpdates];
+
+ NSUInteger count = [_downloadMetadataQuery resultCount];
+ NSMutableArray *urls = [NSMutableArray arrayWithCapacity:count];
+ for ( NSUInteger i = 0; i < count; i++ ) {
+ NSURL *url = [_downloadMetadataQuery valueOfAttribute:NSMetadataItemURLKey forResultAtIndex:i];
+ [urls addObject:url];
+ }
+
+ dispatch_queue_t queue = dispatch_queue_create("downloadcloudfiles", DISPATCH_QUEUE_SERIAL);
+ dispatch_async(queue, ^{
+ __block BOOL stop = NO;
+ unsigned long long toDownload = 0;
+ do {
+ toDownload = 0;
+ @autoreleasepool {
+ for ( NSURL *url in urls ) {
+ NSNumber *fileSizeNumber = nil;
+ NSNumber *percentDownloaded = nil;
+ NSNumber *downloaded = nil;
+
+ [url getResourceValue:&fileSizeNumber forKey:NSURLFileSizeKey error:NULL];
+ [url getResourceValue:&percentDownloaded forKey:NSURLUbiquitousItemPercentDownloadedKey error:NULL];
+ [url getResourceValue:&downloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:NULL];
+
+ unsigned long long fileSize = fileSizeNumber.unsignedLongLongValue;
+ if ( downloaded && !downloaded.boolValue ) {
+ double percentage = percentDownloaded ? percentDownloaded.doubleValue : 100.0;
+ toDownload += percentage / 100.0 * fileSize;
+ }
+ }
+ dispatch_sync(dispatch_get_main_queue(), ^{
+ if ( _downloadProgressBlock ) _downloadProgressBlock(toDownload <= 0, toDownload, &stop);
+ });
+ [NSThread sleepForTimeInterval:1.0];
+ }
+ } while ( toDownload > 0 && !stop );
+ [_downloadProgressBlock release]; _downloadProgressBlock = nil;
+ dispatch_release(queue);
+ });
+
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:_downloadMetadataQuery];
+ [_downloadMetadataQuery stopQuery];
+ [_downloadMetadataQuery release], _downloadMetadataQuery = nil;
+}
+
- (void)checkUninitiatedUploadsForURLs:(NSArray *)urls
{
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
@@ -435,6 +494,9 @@ - (void)dealloc
[_cloudMetadataQuery disableUpdates];
[_cloudMetadataQuery stopQuery];
[_cloudMetadataQuery release], _cloudMetadataQuery = nil;
+ [_downloadMetadataQuery stopQuery];
+ [_downloadMetadataQuery release], _downloadMetadataQuery = nil;
+ [_downloadProgressBlock release]; _downloadProgressBlock = nil;
[_transferProgressMetadataQuery stopQuery];
[_transferProgressMetadataQuery release], _transferProgressMetadataQuery = nil;

0 comments on commit 62ab283

Please sign in to comment.