I am currently storing and retrieving an NSArray in an "old school" fashion by doing encoding and decoding on the array of objects.
I want to move this storage to iCloud.
Is there a simple way to save an array of custom objects to iCloud and retrieve them?
PSEUDOCODE EXAMPLE
allocate mutable array
itereate 5 times
create custom object
add object to mutable array
end iteration
store mutable array to iCloud
...later on...
retrieve mutable array from iCloud
retrieve specific object from mutable array
Yes, you can save any type of data to iCloud, if iCloud is enabled on the device. You have a couple of options:
If the data is small, use the key-value store. This works a lot like NSUserDefaults, but it syncs via iCloud. This is the easiest way but is the most restrictive option and is limited to no more than 1MB of data.
Use the file API. Any file can be synced via iCloud by creating it locally and then using NSFileManager to make it "ubiquitous" (i.e. iCloud synced). On the receiving end you use NSMetadataQuery to locate files in iCloud, and then NSFileManager to download them and to get the latest version.
Use UIDocument, which has its own semantics for saving and syncing data via iCloud.
Related
As of now, my app contains an array and the user can manipulate the order of items in it by dispatching actions. I'm using redux persist and even if I directly change the array elements by editing the source file, the array does not update unless I clear the app cache.
What are my options to update this kind of persistent data after I publish it to the store without forcing users to clear the cache?
What are you looking for is migrations.
How it works:
Load user's previous state from local storage on app start
Update fields you need
Here is a setup example
Can I save data into the realm without making a model or object file and also retrieve from it using swift?
I don't believe this is possible. Realm models define the schema for that object type, and everything going into Realm needs to have a schema.
If you haven't gone over the docs, the Realm academy page could be a good place to start: Realm Academy
It's hard to say without more information in the original question, but if you want to skip defining the model because it's a one-off piece of data, you can consider storing it in UserDefaults. But there are caveats with that, and one of them is that values stored in UserDefaults are not secure. You should never store sensitive information there. Also, UserDefaults can only store certain data types, and storing too much data in UserDefaults can slow down the launch of your app.
More info on user defaults.
I have an array of images that I download from the server. I need to cache them, and then bring them back to the controller from cache . How correct is this done? After NSCache takes only 1 object with a key ? We'll have to generate a key for each photo? I want to understand itself without libraries.
When I sync the user data (if the user request) to iCloud (using NSUbiquitousKeyValueStore),
it syncs correctly. However, when I use 2 different devices, with 2 different apple IDs I see the SAME data on both and I'm still able to sync across them. So multiple users see each others data.
Is there any way to make the syncing user specific or have I made a mistake somewhere?
Some code that I use:
NSUbiquitousKeyValueStore *icloudatas = [NSUbiquitousKeyValueStore defaultStore];
//save to iCloud
[icloudatas setString:[NSString stringWithFormat:#"%i",
[defaults integerForKey:#"lvl"]] forKey:#"lvl"];
//load from iCloud
[defaults setInteger:[[icloudatas stringForKey:#"lvl"] integerValue] forKey:#"lvl"];
[defaults syncronise];
[icloudatas synchronize];
Problem resolved:
I just changed my Apple ID int the App Store, I forgot to change in iCloud.
That resolved my problem, everything is working properly.
You shouldn't be copying the details into NSUserDefaults inside the app. This is how you are mixing up details from different users. Just use NSUbiquitousKeyValueStore on its own.
Well, more realistically, you can copy data, but you shouldn't just always copy all data between both stores.
You should be observing NSUbiquityIdentityDidChangeNotification and NSUbiquitousKeyValueStoreDidChangeExternallyNotification and checking the reason for NSUbiquitousKeyValueStoreAccountChange (from NSUbiquitousKeyValueStoreChangeReasonKey). When you detect that the user account changed you need to 'reset' any local data either to default values or to cloud values (and prevent any update to cloud values before that happens).
I am planning to use Sencha Touch for an application of my company. I read that local storage stores data on client's browser. MY problem is when I build the code to native app using Sencha CMD how does the local storage work? What is the alternative of local storage to keep some data suppose user details (so that user doesn't need to login always).
Advance Thanks
localStorage
Is a very easy to use interface that works synchronous. It stores strings (you can JSON.stringify other Javascript objects) as key-value pairs and you just set the value with a key in this way:
localStorage.setItem('key', 'value');
To read:
var value = localStorage.getItem('key');
which returns value or null if key wasn't found in the storage.
You can use it as an object:
localStorage['key'] = 'value';
var value = localStorage['key'];
however, this is not the recommended approach. In this case undefined will be returned for non-existing items.
More details:
http://www.w3.org/TR/2013/PR-webstorage-20130409/
http://diveintohtml5.info/storage.html
IndexedDB
Database that sort of replaces the now deprecated Web SQL. It works asynchronous and is a bit harder to understand as it uses stores, requests and transactions to deal with the data.
You can store any kind of objects in IndexedDB (Javascript objects, Blobs (files)).
You can also request storage space. User will be alerted if you need more than 5 mb (5 mb is not defined, but seem to be the limit most browsers set before asking user for permission).
See here for details on how you can use it:
http://www.w3.org/TR/IndexedDB/
http://www.html5rocks.com/en/tutorials/indexeddb/todo/
Web SQL
Although, as mentioned, deprecated it is still supported in browsers such as Safari. Is a database that works with SQL-queries and request, and works asynchronous.
Here are more details:
http://www.w3.org/TR/webdatabase/
http://html5doctor.com/introducing-web-sql-databases/
File API
Currently only supported in Chrome. Is a virtual file-system that works asynchronous as IndexedDB but is intended for large files (video and audio files etc.) but can just as easily be used for storing other data.
You can store any kind of objects in File API as Blobs (files). As it is a virtual file system you can navigate using folders.
You can also request storage space here as well (quota).
For details:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
http://www.w3.org/TR/FileAPI/
And of course there is cookies and for IE UserData, but these are very limited.
This example demonstrates how to use local storage with Sencha Touch model objects:
http://www.mysamplecode.com/2012/06/sencha-touch-local-storage-example.html
If you're wanting to switch & sync data from online to offline you might also be interested in this example:
https://www.robertkehoe.com/2012/11/sencha-touch-2-localstorage-example/