Updating sqlite database without XML

前端 未结 4 929
梦如初夏
梦如初夏 2020-12-11 05:00

My app requires data from a sqlite database. It will ship with a version of this database, but I need to update it on a regular basis (most likely once a month). Typically

4条回答
  •  庸人自扰
    2020-12-11 05:37

    I think your problem stems from requiring authentication with your server.

    I recommend the popular AFNetworking library for help with making HTTP calls. You'd subclass AFHTTPClient in order to provide your authentication details, as in this answer.

    Then in your case, you can use AFXMLRequestOperation to download your XML directly. You may then write it to disk. Note you will need to read over the iOS Data Storage Guidelines to see if the Documents folder is appropriate for your use (which I don't think it is; Caches would probably be better).

    Now, the reason your ExampleDelegate code (which looks like it came from here) doesn't work is that ExampleDelegateSuccess and ExampleDelegateFailure are block types, so you can't pass your NSData and NSError * pointers. You need to provide block objects that conform to the signature of the respective types.

    For example:

    ExampleDelegate *download = [[ExampleDelegate alloc] init];
    [download fetchURL:url 
              withCompletion:^(NSData *thedata) {
        NSLog(@"Success! Data length: %d", theData.length);
    
        // Delete old database
        NSError *error;
    
        // You need to declare 'app' here so you can use it in the line below.
        NSURL *destination = [app applicationDocumentsDirectory];
        destination = [destination URLByAppendingPathComponent:@"myDatabase"];
        destination = [destination URLByAppendingPathExtension:@"sqlite"];
        NSLog(@"destination = %@",destination);
        [[NSFileManager defaultManager] removeItemAtPath:[destination path] error:&error];
    
        // Save into correct location
        BOOL success = [fileData writeToURL:destination atomically:YES];
        NSLog(@"File written successfully? %d", success);
    } 
              failure:^(NSError *theError){ 
        NSLog(@"Failure: %@", [theError localizedDescription]}
    ];
    

    The blocks will provide you with the NSData and NSError instances as required, so you don't need to declare your own. You can learn more about blocks here.

    Hope that helps :D

提交回复
热议问题