Browse Source

Add lock on TableAutomaticUpload

Marino Faggiana 8 years ago
parent
commit
d2db13d5da

+ 2 - 2
iOSClient/AppDelegate.m

@@ -1442,7 +1442,7 @@
     if(![ocNetworking automaticCreateFolderSync:folderPhotos]) {
         
         // Activity
-        [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedStringFromTable(@"_not_possible_create_folder_", @"Error", nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
+        [CCCoreData addActivityClient:folderPhotos fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedStringFromTable(@"_not_possible_create_folder_", @"Error", nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
         
         return false;
     }
@@ -1455,7 +1455,7 @@
             if(![ocNetworking automaticCreateFolderSync:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder]]) {
                 
                 // Activity
-                [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedString(@"_error_createsubfolders_upload_",nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
+                [CCCoreData addActivityClient:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder] fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedString(@"_error_createsubfolders_upload_",nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
                 
                 return false;
             }

+ 1 - 1
iOSClient/FileSystem/CCCoreData.h

@@ -189,7 +189,7 @@
 
 // ===== Automatic Upload =====
 
-+ (void)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account;
++ (BOOL)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account;
 + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector context:(NSManagedObjectContext *)context;
 + (void)deleteTableAutomaticUploadForAccount:(NSString *)account identifier:(NSString *)identifier context:(NSManagedObjectContext *)context;
 + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector;

+ 15 - 6
iOSClient/FileSystem/CCCoreData.m

@@ -1501,18 +1501,21 @@
 #pragma mark ===== Automatic Upload =====
 #pragma --------------------------------------------------------------------------------------------
 
-+ (void)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account
++ (BOOL)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account
 {
     NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
+    TableAutomaticUpload *record = nil;
     
-    // Delete record if exists
-    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (identifier == %@)", account, metadataNet.identifier];
-    [TableAutomaticUpload MR_deleteAllMatchingPredicate:predicate inContext:context];
+    // Record exists ?
+    record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (identifier == %@)", account, metadataNet.identifier] inContext:context];
+    if (record)
+        return NO;
     
-    TableAutomaticUpload *record = [TableAutomaticUpload MR_createEntityInContext:context];
+    record = [TableAutomaticUpload MR_createEntityInContext:context];
         
     record.account = account;
     record.identifier = metadataNet.identifier;
+    record.lock = [NSNumber numberWithBool:NO];
     record.date = [NSDate date];
     record.fileName = metadataNet.fileName;
     record.selector = metadataNet.selector;
@@ -1522,6 +1525,8 @@
     record.priority = [NSNumber numberWithLong:metadataNet.priority];
         
     [context MR_saveToPersistentStoreAndWait];
+    
+    return YES;
 }
 
 + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector context:(NSManagedObjectContext *)context
@@ -1529,7 +1534,7 @@
     if (context == nil)
         context = [NSManagedObjectContext MR_context];
     
-    TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector] inContext:context];
+    TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@) AND (lock == 0)", account, selector] inContext:context];
     
     if (record) {
     
@@ -1545,6 +1550,10 @@
         metadataNet.session = record.session;
         metadataNet.taskStatus = k_taskStatusResume;                        // Default
         
+        // LOCK
+        record.lock = [NSNumber numberWithBool:YES];
+        [context MR_saveToPersistentStoreAndWait];
+
         return metadataNet;
     }
     

+ 1 - 0
iOSClient/FileSystem/TableAutomaticUpload+CoreDataProperties.h

@@ -19,6 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
 @property (nullable, nonatomic, copy) NSDate *date;
 @property (nullable, nonatomic, copy) NSString *fileName;
 @property (nullable, nonatomic, copy) NSString *identifier;
+@property (nullable, nonatomic, copy) NSNumber *lock;
 @property (nullable, nonatomic, copy) NSNumber *priority;
 @property (nullable, nonatomic, copy) NSString *selector;
 @property (nullable, nonatomic, copy) NSString *selectorPost;

+ 1 - 0
iOSClient/FileSystem/TableAutomaticUpload+CoreDataProperties.m

@@ -18,6 +18,7 @@
 @dynamic date;
 @dynamic fileName;
 @dynamic identifier;
+@dynamic lock;
 @dynamic priority;
 @dynamic selector;
 @dynamic selectorPost;

+ 9 - 2
iOSClient/PhotosCameraUpload/CCPhotosCameraUpload.m

@@ -1208,13 +1208,20 @@
         metadataNet.session = session;
         metadataNet.taskStatus = k_taskStatusResume;
         
-        [CCCoreData addTableAutomaticUpload:metadataNet account:app.activeAccount];
+        if (![CCCoreData addTableAutomaticUpload:metadataNet account:app.activeAccount]) {
+            
+            [CCCoreData addActivityClient:fileName fileID:metadataNet.identifier action:k_activityDebugActionAutomaticUpload selector:metadataNet.selector note:@"File already present in Table automatic Upload" type:k_activityTypeInfo verbose:k_activityVerboseHigh account:app.activeAccount activeUrl:app.activeUrl];
+            
+            [self endLoadingAssets];
+
+            return;
+        }
         
         // Activity
         NSString *media = @"";
         if (assetMediaType == PHAssetMediaTypeImage) media = @"Image";
         if (assetMediaType == PHAssetMediaTypeVideo) media = @"Video";
-        [CCCoreData addActivityClient:fileName fileID:@"" action:k_activityDebugActionAutomaticUpload selector:@"" note:[NSString stringWithFormat:@"Add TableAutomaticUpload on Session: %@, Set Data asset %@", session, media] type:k_activityTypeInfo verbose:k_activityVerboseHigh account:app.activeAccount activeUrl:app.activeUrl];
+        [CCCoreData addActivityClient:fileName fileID:metadataNet.identifier action:k_activityDebugActionAutomaticUpload selector:metadataNet.selector note:[NSString stringWithFormat:@"Add TableAutomaticUpload on Session: %@, Set Data asset %@", session, media] type:k_activityTypeInfo verbose:k_activityVerboseHigh account:app.activeAccount activeUrl:app.activeUrl];
         
         // Upldate Camera Upload data  
         if ([metadataNet.selector isEqualToString:selectorUploadAutomatic])

+ 4 - 3
iOSClient/cryptocloud.xcdatamodeld/cryptocloud 7.xcdatamodel/contents

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11759" systemVersion="16D32" minimumToolsVersion="Xcode 7.0" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="cryptocloud">
+<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="12141" systemVersion="16E195" minimumToolsVersion="Xcode 7.0" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="cryptocloud">
     <entity name="TableAccount" representedClassName="TableAccount" syncable="YES">
         <attribute name="account" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="active" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" syncable="YES"/>
@@ -56,6 +56,7 @@
         <attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
         <attribute name="fileName" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="identifier" optional="YES" attributeType="String" syncable="YES"/>
+        <attribute name="lock" optional="YES" attributeType="Boolean" usesScalarValueType="NO" syncable="YES"/>
         <attribute name="priority" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
         <attribute name="selector" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="selectorPost" optional="YES" attributeType="String" syncable="YES"/>
@@ -153,13 +154,13 @@
     <elements>
         <element name="TableAccount" positionX="-6597" positionY="111" width="171" height="585"/>
         <element name="TableActivity" positionX="-6399" positionY="252" width="128" height="210"/>
-        <element name="TableAutomaticUpload" positionX="-6597" positionY="801" width="128" height="180"/>
+        <element name="TableAutomaticUpload" positionX="-6597" positionY="801" width="128" height="195"/>
         <element name="TableCertificates" positionX="-6597" positionY="711" width="128" height="60"/>
         <element name="TableDirectory" positionX="-6372" positionY="111" width="162" height="210"/>
+        <element name="TableExternalSites" positionX="-6399" positionY="252" width="128" height="135"/>
         <element name="TableGPS" positionX="-6597" positionY="990" width="128" height="165"/>
         <element name="TableLocalFile" positionX="-6138" positionY="111" width="162" height="225"/>
         <element name="TableMetadata" positionX="-6138" positionY="402" width="162" height="510"/>
         <element name="TableShare" positionX="-6597" positionY="558" width="128" height="120"/>
-        <element name="TableExternalSites" positionX="-6399" positionY="252" width="128" height="135"/>
     </elements>
 </model>