开发者

How - where to backup an app database as restore it on iPhone

What do you do for app database backup?

On other phones that have SD Cards backup of the app database is not a problem as I can make the backup of the app database to the SD Card. I can also send that backup to email as a zip. Restoring is also not a problem as the email can save the zip to 开发者_StackOverflow社区the SD Card use a file that i still on the SD Card.

iPhone seems to be another story. I can make a copy of the user part of the database and store it in the same documents folder that the app resides. I could also email a zip of that file in case the phone dies.

but how would I save a download from an email attachment to the app document folder? so if the user uploaded their DB to their email, and then wants to restore it, how would you do this?

What do you all do for DB backups from your apps?


If you want to save the document from an email you could do the following:

First, modify your Info.plist file to register to handle the specific extension of your backup file by adding it to a new key called CFBundleDocumentTypes.

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>MyApplication</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.myapplication.myapplicationsuffix.fileextension</string>
        </array>
    </dict>
</array>

The next thing to set up is your application's bundle URL Types.

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.myapplication.myapplicationsuffix.myoperationkey</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>packageforlaunch</string>
        </array>
    </dict>
</array>

That way when the user launches the file (taps on it in the email), it will launch your application and provide a filesystem URI to it. In your app delegate, you can implement the -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url method.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
     if(url != nil && [url isFileURL])
    {
        NSData *d = [NSData dataWithContentsOfURL:url];
        [[NSFileManager defaultManager] createFileAtPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[url lastPathComponent]] contents:d attributes:nil];
}

That will give you the file and you can save it to your application. The key here is to not have your database stored in the application bundle, but instead in your application's user folder somewhere. That way you can easily replace it ( as long as you have the proper locking in place ) from an application launch. With CoreData, you have to be careful that you have not changed the schema, or that you are loading the correct .xcdatamodeld files for the database version, or you could end up with an un-openable database.

What I have done is to use the SQLite data store for CoreData, or to use SQLite by itself. Lock the file briefly, create a copy of the database file and upload it via PUT / POST to my server via an API. The server then stores the file in CouchDB with the timestamp. This has the effect of being a poor man's versioning system, you could always roll back since the database is tagged with either the user's id, or the user's UDID, or both with the timestamp.

The simpler method will likely be to use a combination of Apple's new document store for the database, and the key/value store with a pointer to the document id that represents the backup.

Unfortunately, I don't think there is an easy mechanism for this, the email mechanism you describe is probably the simplest, but with CoreData, it makes even that complicated. If you are using raw SQLite3 and the FMDB library, it makes things much easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜