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

后端 未结 3 426
盖世英雄少女心
盖世英雄少女心 2021-01-15 16:55

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

相关标签:
3条回答
  • 2021-01-15 17:08

    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/

    0 讨论(0)
  • 2021-01-15 17:21

    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:];
    
    0 讨论(0)
  • 2021-01-15 17:26

    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:enter image description here

    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!

    0 讨论(0)
提交回复
热议问题