From a32edbe73c0085b367c0e75d251531cbb4ea3041 Mon Sep 17 00:00:00 2001 From: Yuji Tachikawa Date: Mon, 3 Sep 2018 13:49:03 +0900 Subject: [PATCH] debugging --- ArticleListArchiveAdditions.m | 2 +- SyncManager.m | 90 ++++++++++++------------------- iCloudHelper.h | 7 ++- iCloudHelper.m | 33 ++++++++++-- inspire.xcodeproj/project.pbxproj | 6 +++ 5 files changed, 75 insertions(+), 63 deletions(-) diff --git a/ArticleListArchiveAdditions.m b/ArticleListArchiveAdditions.m index 6f22a11..06521d0 100644 --- a/ArticleListArchiveAdditions.m +++ b/ArticleListArchiveAdditions.m @@ -217,7 +217,7 @@ +(void)populateFlaggedArticlesFrom:(NSArray*)a usingMOC:(NSManagedObjectContext* } +(NSArray*)notFoundArticleListsAfterMergingChildren:(NSArray*)children toArticleFolder:(ArticleFolder*)af usingMOC:(NSManagedObjectContext*)secondMOC { - NSLog(@"merging to folder:%@",af?af.name:@"toplevel"); +// NSLog(@"merging to folder:%@",af?af.name:@"toplevel"); NSArray*articleLists=[af.children allObjects]; NSMutableArray*seen=[NSMutableArray array]; if(!articleLists){ diff --git a/SyncManager.m b/SyncManager.m index c0c322e..84fb09e 100644 --- a/SyncManager.m +++ b/SyncManager.m @@ -20,16 +20,17 @@ #define SYNCDATAEXTENSION @"inspireSidebarContents" + @interface ReadSnapshotFromFileOperation:ConcurrentOperation --(instancetype)initWithFileName:(NSString*)f; +-(instancetype)initWithFileName:(NSURL*)f; @property NSDictionary*snapShot; @end @implementation ReadSnapshotFromFileOperation { - NSString*file; + NSURL*file; } --(instancetype)initWithFileName:(NSString*)f +-(instancetype)initWithFileName:(NSURL*)f { self=[super init]; file=f; @@ -38,23 +39,18 @@ -(instancetype)initWithFileName:(NSString*)f -(void)run { #if TARGET_OS_IPHONE - NSArray*versions=[iCloudHelper findUnresolvedConflictingVersionsOfFile:file]; - NSFileVersion*latestVersion=versions[0]; - NSDate*latestDate=latestVersion.modificationDate; - for(NSFileVersion*version in versions){ - NSLog(@"%@ versions found.",@(versions.count)); - if([version.modificationDate laterDate:latestDate]){ - latestVersion=version; - latestDate=latestVersion.modificationDate; - } - } - [iCloudHelper resolveConflictForFile:file withSelectedFileVersion:latestVersion]; - [iCloudHelper retrieveCloudDocumentWithName:file completion:^(NSData *data) { - self.snapShot=[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:NULL]; - [self finish]; - }]; + dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0),^{ + [iCloudHelper retrieveCloudDocumentWithName:file completion:^(NSData *data) { + if(data){ + self.snapShot=[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:NULL]; + }else{ + self.snapShot=nil; + } + [self finish]; + }]; + }); #else - NSData*data=[NSData dataWithContentsOfFile:file]; + NSData*data=[NSData dataWithContentsOfURL:file]; self.snapShot=[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:NULL]; [self finish]; #endif @@ -137,8 +133,9 @@ -(instancetype)init NSArray*a=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:listsSyncFolder error:NULL]; for(NSString*fileName in a){ if([fileName hasSuffix:SYNCDATAEXTENSION]){ - NSDate*date=[[[NSFileManager defaultManager] attributesOfItemAtPath:[listsSyncFolder stringByAppendingPathComponent:fileName] error:NULL] fileModificationDate]; - [self stateChanged:fileName atDate:date]; + NSString*fullPath=[listsSyncFolder stringByAppendingPathComponent:fileName]; + NSDate*date=[[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL] fileModificationDate]; + [self stateChanged:[NSURL fileURLwithPath:fullPath] atDate:date]; } } }); @@ -156,16 +153,7 @@ -(void)startedGathering:(NSNotification*)n } -(void)finishedGathering:(NSNotification*)n { - NSArray*items=query.results; - for(NSMetadataItem*item in items){ - NSURL*f =[item valueForKey:NSMetadataItemURLKey]; - NSString*fileName=[f lastPathComponent]; - if([fileName hasSuffix:SYNCDATAEXTENSION]){ - NSDate*date=nil; - [f getResourceValue:&date forKey:NSURLContentModificationDateKey error:NULL]; - [self stateChanged:fileName atDate:date]; - } - } + [self update:n]; NSOperation*op=[NSBlockOperation blockOperationWithBlock:^{ dispatch_async(dispatch_get_main_queue(),^{ [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"initialMergeDone"]; @@ -176,25 +164,18 @@ -(void)finishedGathering:(NSNotification*)n -(void)update:(NSNotification*)n { NSArray*items=query.results; - NSDate*latest=[NSDate dateWithTimeIntervalSince1970:0]; - NSMetadataItem*latestItem=nil; for(NSMetadataItem*item in items){ - NSString*fileName=[item valueForAttribute:NSMetadataItemFSNameKey]; - if(![fileName hasSuffix:SYNCDATAEXTENSION]) + NSURL*f =[item valueForKey:NSMetadataItemURLKey]; + if(![f isStatusCurrent]){ + continue; + } + if(![f.absoluteString hasSuffix:SYNCDATAEXTENSION]) continue; - NSString*targetMachineName=[self machineNameFromSaveFileName:fileName]; + NSString*targetMachineName=[self machineNameFromSaveFileName:f]; if([targetMachineName isEqualToString:self.machineName]) continue; NSDate*fileDate=[item valueForAttribute:NSMetadataItemFSContentChangeDateKey]; - if([fileDate timeIntervalSinceDate:latest]>0){ - latestItem=item; - latest=fileDate; - } - } - if(latestItem){ - NSString*fileName=[latestItem valueForAttribute:NSMetadataItemFSNameKey]; - NSDate*date=[latestItem valueForAttribute:NSMetadataItemFSContentChangeDateKey]; - [self stateChanged:[fileName lastPathComponent] atDate:date]; + [self stateChanged:f atDate:fileDate]; } } #endif @@ -214,8 +195,9 @@ -(NSString*)machineName -(NSString*)saveFileName{ return [NSString stringWithFormat:@"%@.%@",self.machineName,SYNCDATAEXTENSION]; } --(NSString*)machineNameFromSaveFileName:(NSString*)newFile{ - NSMutableArray*a=[[newFile componentsSeparatedByString:@"."] mutableCopy]; +-(NSString*)machineNameFromSaveFileName:(NSURL*)newFile{ + NSString*lastComponent=[newFile lastPathComponent]; + NSMutableArray*a=[[lastComponent componentsSeparatedByString:@"."] mutableCopy]; [a removeLastObject]; NSString*targetMachineName=[a componentsJoinedByString:@"."]; return targetMachineName; @@ -254,13 +236,12 @@ -(void)modifiedFileAtPath:(NSString *)file { NSLog(@"noted:%@",file); if([file hasSuffix:SYNCDATAEXTENSION]){ - NSString*fileName=[file lastPathComponent]; - NSDate*date=[[[NSFileManager defaultManager] attributesOfItemAtPath:fileName error:NULL] fileModificationDate]; - [self stateChanged:fileName atDate:date]; + NSDate*date=[[[NSFileManager defaultManager] attributesOfItemAtPath:file error:NULL] fileModificationDate]; + [self stateChanged:[NSURL fileURLWithPath:file] atDate:date]; } } --(void)stateChanged:(NSString*)newFile atDate:(NSDate*)date{ +-(void)stateChanged:(NSURL*)newFile atDate:(NSDate*)date{ NSString*targetMachineName=[self machineNameFromSaveFileName:newFile]; if([targetMachineName isEqualToString:[self machineName]]) return; @@ -281,12 +262,7 @@ -(void)stateChanged:(NSString*)newFile atDate:(NSDate*)date{ PrepareSnapshotOperation*pso=[[PrepareSnapshotOperation alloc] init]; [queue addOperation:pso]; -#if TARGET_OS_IPHONE - NSString*newFileX=newFile; -#else - NSString*newFileX=[listsSyncFolder stringByAppendingPathComponent:newFile]; -#endif - ReadSnapshotFromFileOperation*rso=[[ReadSnapshotFromFileOperation alloc] initWithFileName:newFileX]; + ReadSnapshotFromFileOperation*rso=[[ReadSnapshotFromFileOperation alloc] initWithFileName:newFile]; [queue addOperation:rso]; MergeSnapShotOperation*mso=[[MergeSnapShotOperation alloc] initWithPSO:pso andRSO:rso forTargetMachineName:targetMachineName]; [queue addOperation:mso]; diff --git a/iCloudHelper.h b/iCloudHelper.h index e67c2e3..cbe16df 100644 --- a/iCloudHelper.h +++ b/iCloudHelper.h @@ -9,12 +9,17 @@ // completion handlers can be called from non-main thread. +@interface NSURL (iCloudAddition) +-(BOOL)isStatusCurrent; +@end + + @interface iCloudHelper : NSObject +(BOOL)iCloudAvailable; +(void)setupWithUbiquityContainerIdentifier:(NSString*)container completion:(void(^)(NSURL*ubiquityContainerURL))handler; +(NSMetadataQuery*)metadataQueryForExtension:(NSString*)extension; + (void)saveAndCloseDocumentWithName:(NSString *)documentName withContent:(NSData *)content completion:(void (^)(BOOL success))handler __attribute__((nonnull)); -+ (void)retrieveCloudDocumentWithName:(NSString *)documentName completion:(void (^)(NSData *documentData))handler __attribute__((nonnull)); ++ (void)retrieveCloudDocumentWithName:(NSURL *)documentName completion:(void (^)(NSData *documentData))handler __attribute__((nonnull)); /** Find all the conflicting versions of a specified document diff --git a/iCloudHelper.m b/iCloudHelper.m index 45e1a61..b455f24 100644 --- a/iCloudHelper.m +++ b/iCloudHelper.m @@ -8,6 +8,23 @@ #import "iCloudHelper.h" +@implementation NSURL (iCloudAddition) +-(BOOL)isStatusCurrent +{ + NSString*status=nil; + [self getResourceValue:&status forKey:NSURLUbiquitousItemDownloadingStatusKey error:nil]; + BOOL isCurrent=[status isEqualToString:NSURLUbiquitousItemDownloadingStatusCurrent]; +// NSLog(@"%@:%@",status,self); + if(!isCurrent){ + [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:self error:nil]; + return NO; + }else{ + return YES; + } +} +@end + + // // iCloudDocument.h // iCloud Document Sync @@ -157,18 +174,26 @@ + (void)saveAndCloseDocumentWithName:(NSString *)documentName withContent:(NSDat } }]; } -+ (void)retrieveCloudDocumentWithName:(NSString *)documentName completion:(void (^)(NSData *documentData))handler __attribute__((nonnull)) ++ (void)retrieveCloudDocumentWithName:(NSURL *)fileURL completion:(void (^)(NSData *documentData))handler __attribute__((nonnull)) { // @try { // Get the URL to get the file from - NSURL *fileURL = [[self ubiquitousDocumentsDirectoryURL] URLByAppendingPathComponent:documentName]; - + NSString*status; + [fileURL getResourceValue:&status forKey:NSURLUbiquitousItemDownloadingStatusKey + error:NULL]; +// NSLog(@"%@:%@",status,fileURL); + if(![status isEqualToString:NSURLUbiquitousItemDownloadingStatusCurrent]){ + NSLog(@"%@ is not downloaded",fileURL); + handler(nil); + return; + } + + // If the file exists open it; otherwise, create it if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) { // Create the UIDocument object from the URL iCloudDocument *document = [[iCloudDocument alloc] initWithFileURL:fileURL]; - if (document.documentState & UIDocumentStateClosed) { [document openWithCompletionHandler:^(BOOL success){ diff --git a/inspire.xcodeproj/project.pbxproj b/inspire.xcodeproj/project.pbxproj index ad27ff0..3fde769 100644 --- a/inspire.xcodeproj/project.pbxproj +++ b/inspire.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ 331D7E301B7A32F500F598B5 /* ArticleListArchiveAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 331D7E2F1B7A32F400F598B5 /* ArticleListArchiveAdditions.m */; }; 331E6EFB0FFE83CB00F07C9A /* SpiresQueryDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 331E6EFA0FFE83CB00F07C9A /* SpiresQueryDownloader.m */; }; 331FD3310F36B11800C16F73 /* MainTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 331FD3300F36B11800C16F73 /* MainTableViewController.m */; }; + 3322B028213CCBF400AA123D /* iCloudHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 3322B027213CCBF400AA123D /* iCloudHelper.m */; }; 33255AAB0F3131F500538063 /* Acknowledgments.html in Resources */ = {isa = PBXBuildFile; fileRef = 33255AAA0F3131F500538063 /* Acknowledgments.html */; }; 332824940F560DE800AA6E00 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 332824930F560DE800AA6E00 /* dsa_pub.pem */; }; 332873C01BAA47F3005F8EC7 /* SpecificArticleListTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 332873BF1BAA47F3005F8EC7 /* SpecificArticleListTableViewController.m */; }; @@ -250,6 +251,8 @@ 331FD3300F36B11800C16F73 /* MainTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MainTableViewController.m; sourceTree = ""; }; 33202EE010D9D8EA00EDB5A8 /* Migrator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Migrator.h; sourceTree = ""; }; 33202EE110D9D8EA00EDB5A8 /* Migrator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Migrator.m; sourceTree = ""; }; + 3322B026213CCBF400AA123D /* iCloudHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = iCloudHelper.h; sourceTree = ""; }; + 3322B027213CCBF400AA123D /* iCloudHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = iCloudHelper.m; sourceTree = ""; }; 33255AAA0F3131F500538063 /* Acknowledgments.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Acknowledgments.html; sourceTree = ""; }; 332824930F560DE800AA6E00 /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = ""; }; 332873BE1BAA47F3005F8EC7 /* SpecificArticleListTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecificArticleListTableViewController.h; sourceTree = ""; }; @@ -863,6 +866,8 @@ 33CD56872137C582007C7943 /* Sync */ = { isa = PBXGroup; children = ( + 3322B026213CCBF400AA123D /* iCloudHelper.h */, + 3322B027213CCBF400AA123D /* iCloudHelper.m */, 331D7E2E1B7A32F400F598B5 /* ArticleListArchiveAdditions.h */, 331D7E2F1B7A32F400F598B5 /* ArticleListArchiveAdditions.m */, 33441FC01B79DC0F00A26198 /* SyncManager.h */, @@ -1210,6 +1215,7 @@ 33BBA3BE1B8F3AE20001366C /* NetworkOperationQueue.m in Sources */, 33BBA3AB1B8F3A910001366C /* ArticleData.m in Sources */, 33C9E4421B92CBE00059F0F8 /* AddArticleFolderViewController.m in Sources */, + 3322B028213CCBF400AA123D /* iCloudHelper.m in Sources */, 33BBA3A61B8F3A910001366C /* CannedSearch.m in Sources */, 332FD069212D26D300D2EA01 /* PDFViewController.m in Sources */, 33BBA3BB1B8F3AE20001366C /* ArxivNewArticleListReloadOperation.m in Sources */,