开发者

Can I save user's drafts using NSUserDefaults?

I am trying to save drafts using NSUserDefaults but I am not sure if it is the right place to save data not user's preferences in.

My application allows user to choose 1 or 2 images to upload and share. Users can save draft and come back to upload later. My current approach is to save the chosen UIImage to /Library directory and save the file path to NSUserDefaults.

The data structure is NSUserDefaults -> Draft NSDictionary -> key - NSArray. I have many drafts (just like email drafts). The Draft Dictionary contains a saved date string as a key and string file paths in NSArray. The saving process doesn't have any problems currently. But because in NSUserDefaults doc, they say:

The defaults system allows an application to customize its behavior to match a user’s preferences.to match a user’s preferences.

So, I don't know if my approach is good or not, any possible future problems with it?

Do you suggest me 开发者_Python百科any simple solution that can do this well without having to code much. I know there is CoreData but I don't know much about it. Is it easy to learn and implement?

I don't know if this can affect some memory problem or not, but I expect that a user may save drafts less than 100.


You don't want to save any actual data to NSUserDefaults, however, it acceptable to save the previous application state in the defaults such that the app can resume where it left off.

Your data structure seems overly complex. You can just save an array of file paths directly to the user defaults.

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSArray *draftPaths=[NSArray arrayWithObjects:filePath1, filePath2,nil];
[defaults setObject:draftPaths forKey:@"DraftPaths_Key"];
// to retrieve
NSArray *previousDraftPaths=[defaults objectForKey:@"DraftPaths_Key"];

You don't want to save the files into the cache folders because the system can delete caches if space gets tight. Instead, save them to custom folder in the /Library directory. You can get the path to the library with:

NSArray *libraryPaths=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath=[libraryPaths objectAtIndex:0];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜