简体   繁体   中英

How to upload and store files in Cocoa with Core Data?

I have an app which I would like to be able to upload files to. I could imagine serializing the data, putting that into an array, then serializing the array and putting that into Core Data but that doesn't quite seem right. I could also imagine copying the files into the application's supporting files, getting the NSURL for those and storing the array or NSURLs.

In any case, NSURL is not an attribute option, nor is NSArray, in Core Data.

Another potential issue - my goal is to make an iPhone App and Desktop App that sync together. So I want to upload images from my iPhone or upload files from my desktop and have everything work fine together. I'm not sure copying the files over would be compatible with iPhone. I want to be able to upload any type of file as well.

THE QUESTION:
How do I store images on iOS?
How do I store files on Mac OS X?
Do I store binary data in core data? Do I copy files into a directory and store an NSURL?

BONUS QUESTION:
How do I sync between iOD and Mac OS X applications?

CLARIFICATION:
Sorry about any confusion from this question. Here's a cleaner version:

My Core Data Model consists of an "Entry" entity that has a name NSString attribute and a one-to-many relationship with a "File" entity. This file entity has a name NSString attribute and a second attribute that has yet to be determined. This second attribute is maybe (from what I can come up with) a transformable NSData representation of some file, a transformable representation of NSURL that points to some file (I am still uncomforable with how transformable works, so I could be wrong about all this...), or a binary blob of the file data. There may be other choices, but the point is is to be able to save or "upload" files to this program from either OS X or iOS and sync the databases together.

I have an OS X Application with a table view that displays a names of all the Entry's. Select the name of an Entry and you will have a list of Files that it points to. Double click the file and it should open with the default application just as it would with Finder. You should also be able to export the files out of Core Data and save them back to a directory outside of the app.

I also have an iOS Application that allows me to take a picture or multiple pictures, save them to "File" entities, point to those files from an "Entry" entity (using the same Core Data model as the OS X Application), and offine sync with my OS X Application so both applications have all the same data.

I hope that clears things up.

Thanks for the help,

Chet

I believe you're not very much familiar with Core Data yet. I would strongly suggest to dive into Core Data documentation.

Anyway, from what I understand, possible implementation of your data model could look like this: 在此输入图像描述

Your user would have an NSSet of File objects, each File object would be responsible for storing the name and its URL in Application Documents directory.

As for your second issue, I don't quite see a question there.

Hope this helps, Cheers!

Core Data allows you to set an option for external storage of attributes and IT will then decide whether to store the object in an external file.

So you would create an attribute called fileData (binary) and in your subclass use NSData and then simply set the file.fileData = yourFileData and Core Data will decide where to store it.

No need to track the URL yourself. Don't forget to store some kind of identifier (file name) to allow you to retrieve the file you want.

EDIT

BTW if you just want to store an image of attributed string then it might be better to use a transformable attribute. Transformable is useful if you want automatic transformation to NSImage, UIImage or NSAttributedString (or some other archivable object).

set the file like this

NSData * data = [NSData dataWithContentsOfURL:...];
file.fileData = data;

To write the data to a file use

bool result = [file.fileData writeToURL:atomically:];

To sync core data between devices I would suggest you take a look at this site where I have posted details on Core Data and iCloud integration for both iOS and OS X apps. It is beyond the scope of an answer on this site.

http://ossh.com.au/design-and-technology/software-development/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM