Saving and Loading data in a Sandbox - OS X - App 2016?

When I saving User Settings in iOS [Swift], I’m using NSUserDefaults.

Now making a app for OS X, I’m wondering what is the best way in XOJO? In the past I made a .xml file in a .hidden folder in the user-folder, load and save the xml. That was easy. But with Sandboxing in MacAppStore, I’ll get problems. What is the best way - in 2016 - to make this? A “bridge” to NSUserDefaults? Or is there existing a simple solution in Xojo [without plugins].

Simply save into a folder you create in SpecialFolder.AppData, for instance com.myapplication.gr8app.

No need for special declare. Normal Streams work just fine.

Thank you :slight_smile:

Or just save your preferences in NSUserDefaults like you do on iOS. That’s where NSUserDefaults is for.

@Marco Hof: Thank you. But then I have to use the MacOSLib? I want to avoid third-party plugins. I have used the MacOSLib in the past with Xojo 2014.1, and now I can’t upgrade a app to a newer xojo version because the MacOSLib changed so much, I modified the original MacOSLib, so I don’t can upgrade without problems. So I want to avoid third-party plugins, to avoid this problems again. :slight_smile:

I think Michels solution is the best :slight_smile:

I have been using my own preference file the way I described for ten years or so. The nice thing is I can save all sorts of things, in the format I want. In my latest app, I use a dictionary to store all kinds of data, such as windows positions, controls position (the user can move them), field’s content, and so on. Very convenient.

Yes, it’s the perfect way. I think I will use a JSON - Format to store the data in the local file, than I can reuse the JSON - when I need it in the future for other things. :slight_smile:

[quote=283766:@Michel Bujardet]Simply save into a folder you create in SpecialFolder.AppData, for instance com.myapplication.gr8app.

No need for special declare. Normal Streams work just fine.[/quote]
It’s important to use this folder, NEVER EVER directly work with the preferences folder as mysterious things happen to files in that folder. Apple also advised against directly manipulating files in the preferences folder.

JSON is brilliant for storing small chunks of data, however you may want to reconsider this unless you know that your application is only going to store small amounts of data. It becomes very slow very quickly.

I personally would recommend creating a binary format. If you had a dictionary full of strings, you could use something similar to below.

stream.littleEndian = true Dim keys() as variant = dic.keys Dim value as variant For each key as variant in keys value = dic.value( key ) stream.writeUint16 lenb( key ) stream.write key stream.writeUint8 1 // --- indicates the value is a string, use other numbers for different value types. stream.writeUint32 lenb( value ) stream.write value next

Xojo.Core.Data has an interesting GenerateJSON method that turns a Xojo.Core.Dictionary in JSon very quickly, and ParseJSon, to do the reverse. I am considering using that in the future.

@Michel Bujardet : the old Dictionary has similar methods. Works nicely for simple data types and chokes on dates for instance.

Well, for preferences, it will probably be strings, and the date issue can be worked around with string formats as well.

If you keep the quantity down low. You can also store dates as a pair of doubles. One for the totalseconds and one for GMTOffset.

I have an application that uses JSON for the data part of it’s files, and I was storing encoded Security-Scoped Bookmarks (so that the application can link back to the original file if it likes), it was making the save routine go from being instantaneous to taking two seconds.

So I switched a stored the raw Security-Scoped Bookmark data directly in the data file (as binary data) and it’s back to being pretty instantaneous.

I’m not against JSON, I use it a lot for small amounts of data (especially with the dictionary like operation). Just want to make it clear that they’re not good IF your app might be storing medium to large amounts of data or Security-Scoped Bookmarks.