1

This is probably a naive question - but I want to double check to avoid wasting time with UIDocument if it doesn't do what I want.

Background: I have an app for which I have created a simple file system to save out user created documents as plists. I have my encoding/decoding all working. I am using some primitive types and handle that with the appropriate encoder method. I have a naming system and save the plists to a custom directory in the Library directory since these are docs the user should not have direct access to. (they can export their data to the documents directory if they so choose.

I started thinking about "autosave" and then discovered UIDocument - looks pretty great.

So given the above, does it seem like I can use UIDocument? What I save out is a custom class "Project" instance derived from NSObject. It contains a bunch of NSMutable arrays which contain instances of custom classes, NSDictionaries etc. I'm going through this UIDocument tutorial now: http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1 but don't want to discover that it's not going to work because of my data, etc.

Update:(for those reading along at home... ;-) Made some progress with this. UIDocument uses NSKeyedArchiver rather than NSCoder (Wrong - see answer below) but the encoding method names are the same so it was easy to adjust what I already had. Have been able to save out a plist that looks like it is capturing all the data - but I won't know until I try to read it all back in. Getting an error that I haven't sorted out:

NSFileCoordinator: A surprising server error was signaled. Details: Connection invalid

Not so surprising since I am saving locally not clear why it is trying to connect to iCloud at all. Hopefully I can switch that off.

spring
  • 18,009
  • 15
  • 80
  • 160

1 Answers1

2

I'm not sure where you get the thing about UIDocument using NSKeyedArchiver. For a simple implementation all you need to do is provide an NSData representation of your document contents -- it doesnt matter whether you generate that data from your model objects with NSCoder, NSKeyedArchiver, NSPropertyListSerialization, or some custom scheme.

Given that, I don't see any reason it shouldn't work with the data model you describe.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Was following the code here: http://stackoverflow.com/questions/8479198/icloud-how-to-save-archive-containing-array-of-custom-objects - didn't find many other examples of using complex data with UIDocument – spring Mar 31 '12 at 01:44
  • 1
    The point is that all UIDocument cares about is getting an NSData -- it doesn't care what's in the data or how it got there. You were making an NSData before with NSCoder, so you can use the same data with UIDocument. (Doesn't mean you're immune from seeing other problems with UIDocument, but at least they won't have to do with your model design. Post some details/code and we can help diagnose.) – rickster Mar 31 '12 at 02:04
  • Cool - thanks for the clarification. I've got it working crudely now. Issues now more @ that I didn't design my classes with a "document" model in mind- so I have some revising to do. Any thoughts on the ` NSFileCoordinator: A surprising server error was signaled. Details: Connection invalid` error? – spring Mar 31 '12 at 07:18
  • Sorry, haven't seen that before. Maybe post it as a new question? – rickster Apr 01 '12 at 03:41