1

I am trying to upload sqlite file on iCloud. I am using NSFileCoortinator for coping the file from one file to another file but schema of sqlite file is only get copied, I am not getting data in sqlite file….Also sometimes the data is appearing in the sqlite file on iCloud. I don’t understand why this is happening. Is this sqlite file issue or I did wrong in code. If anyone has solution please help me out.

-(void)uploadFile
{
    //-------code for uploading data to icloud
    NSArray *paths =
    NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
                                    NSUserDomainMask, YES);
    NSString *applicationSupportDirectory = [paths firstObject]

    NSString *str=[NSString stringWithFormat:@"%@/XMPPMessageArchiving.sqlite",applicationSupportDirectory];        //copy this file
    NSString *fileName = [NSString stringWithFormat:@"XMPPMessageArchiving.sqlite"];
    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];   

    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    NSURL *fromURL = [NSURL fileURLWithPath:str];
    NSURL *toURL = [[ubiq URLByAppendingPathComponent:@"Documents"]
                            URLByAppendingPathComponent:fileName];;


    [coordinator coordinateReadingItemAtURL:fromURL options:0 writingItemAtURL:toURL options:NSFileCoordinatorWritingForReplacing error:nil byAccessor:^(NSURL *newReadingURL, NSURL *newWritingURL)
    {
         NSError *error=nil;
         NSData *data = [[NSFileManager defaultManager] contentsAtPath:fromURL.path];

         //if file is already present on icloud replace that file with new file
         BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:newWritingURL.path];

         if (fileExists)
         {
             [[NSFileManager defaultManager] removeItemAtPath:toURL.path error:NULL];
         }


         BOOL isCopied=  [[NSFileManager defaultManager] copyItemAtPath:newReadingURL.path toPath:newWritingURL.path error:nil];

     NSLog(@"file copied= %d",isCopied);

  }];
}

The value for file copied is always 1 but still data is not available.

nilam_mande
  • 931
  • 7
  • 14
swapnali patil
  • 304
  • 2
  • 17
  • Not sure what you are trying to achieve but how do you know there is not data in the file you copied to iCloud? You do know that this is not the correct way to sync Core Data I presume. Also depending on the Core Data store you may need to copy the *.WAL file as well (write ahead log) or force a sync before closing the store before you copy it. Alternately use JOURNAL mode to avoid the need to copy multiple files. – Duncan Groenewald Aug 07 '15 at 12:53
  • I have changed the journal mode by this line NSDictionary *options = @{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}}; but I dont no is it write way or not? – swapnali patil Aug 07 '15 at 13:03
  • options = [NSPersistentStoreUbiquitousContentNameKey:iCloudFilename, NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption:true, NSSQLitePragmasOption:["journal_mode" : "DELETE" ]] – Duncan Groenewald Aug 08 '15 at 07:48
  • @Duncan Groenewald... Actually I have xmppCoreDataStorage file in that inside this method I have chaged the option & this is not creating .sqlite-wal file - (BOOL)addPersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)theStoreOptions error:(NSError **)errorPtr...But I Dont have file path in this method.... can you please elaborate this answer or share some good link so that I refere that to understand – swapnali patil Aug 08 '15 at 09:31
  • What exactly are you trying to achieve? Sync via iCloud or just copy the sqlite file to iCloud ? Currently it looks like you are just trying to copy the file to iCloud using NSFileManager, that's not the same as sync'ing via iCloud. I have some sample apps that do both here http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ – Duncan Groenewald Aug 09 '15 at 22:52

0 Answers0