NsmutableArray don't succeded to save at close of app - ios

Hello I can not save locally where I have to save an array of strings, I add that this array must be saved when closing the application for iOS and the reopening needs to be recharged, I found thousands of guides online but none are very specific, someone says use NSUserDefaults others say to use NSCoding but in both cases i can not recharge these data can anyone help me?

To Save SMALL data - NSUserDefaults, NSCoding as suggested by other people or saving in plist also or else create a file in document directory and write the contents of array in that file.
LARGE Data- Save it in database using either sqlite or core data.
Choice depends on the requirement of the application and since you have not given the details of the data like maximum number of strings in the array, everyone can suggest you instead of telling a solution. Hope it helps :)

You can use NSUserDefaults:
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:#"Key"];
To get the value from NSMutableArray:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"Key"]];
Hope this is helpful.

Related

NSUserDefaults in iOS is randomly disappearing and reappearing

Just wondering if anybody experience this issue?
I am developing an application in iOS using Objective-C at the moment.
Sometimes my data in NSUserDefaults will be missing after I compile the app.
But if I ignore it and recompile the app again the data suddenly reappears.
I already synchronized in several places (not in every key, but only in several places).
If anyone happened to face this issue before I hope you can share how to handle this issue.
P.S. I need a storage to save 1 particular object so I can retrieve it when the app reopens.
Edited to add the code
NSString *enPIN = [[NSString alloc]initWithString:[NSString stringWithFormat:#"%#", [enterField.text md5]]];
[[NSUserDefaults standardUserDefaults] setObject:enPIN forKey:#"pin"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"check pin %#", [[NSUserDefaults standardUserDefaults] objectForKey:#"pin"]);
The object is a string, i hash it using md5 and then store it in nsuserdefault, if it only randomly dissapearing maybe its not weird, but its also reapearing again after it dissapear if i recompile the apps
Check if your defaults are using only string value or bool or such.
If you are using any Object with Class (key and parameters) like NSObject to store in defaults I prefer you do the encoding and decoding accordingly before storing and retrieving the values.
Also If you storing any NSDictionary check if any of the object value inside that dictionary is not anything other than Bool, String , if there also any NSObject class or reference is stored then you may face same issue.
Refer this stackoverflow link as to how encode objects before storing to NSUserDefaults.
Lastly [defaults synchronise] call mandatory on viewwilldisappear or immediately after storing new value whichever way is your implementation.
Hope this helps.
I had a similar issue the other day with NSUserDefaults
Not quite sure what was causing it, but it was due to a bug in Xcode. I was able to fix the issue without changing my code at all. I simply cleaned the project (CMD-Shift-K) and restarted my computer, and then it worked just fine. It's worth a try
Are you getting any kind of error messages in the console?

iOS Store Data Locally

I'm making an app with a list of the products of a company. The data does not need to be modified by the user. How can I save this information? The data must already exist locally when you download the app.
As per your requirement Plist or SQLite is good option for you.
Why Plist
Because it is lightweight.
Why SQLite
If you want to perform query on your data.
You can save the data in sqlite or coredata. For already filling the data in database you can use "sqlite manager" and run your queries in sqlite manager and save it on desktop(where you want). After create the filled database drag it into your project and do whatever your want. You have already filled database here.
You can go with either NSUserdefaults or CoreData, additionally there is a third party library called Realm.
Also check this question:
storing data locally on the iphone
Edit
The answer was provided by Sr.Richie in the link:
For simple data you should use NSUserDefaults. CoreData is very cool but mainly to store DB structures, and introduces complexity (but i love it:)). If you just need to store String, Array and so on (basically prefs), you can go with NSUserDefaults:
For example:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //load NSUserDefaults
NSArray *fakeFavs = [[ NSArray alloc] initWithObjects:#"2",#"4", #"100", nil]; //declare array to be stored in NSUserDefaults
[prefs setObject:fakeFavs forKey:#"favourites"]; //set the prev Array for key value "favourites"

iOS: What are the Advantages / Disadvantages of using plist or SQLite, as the data source

I am new to cObject or App world, I'm about to make an app where I need to store some information on some players (football), but I do not know where to store them and will hear what you think is best ?
I need to store data like name, age, etc.. I know how I'm doing some text boxes so I can enter the data, but do not know where to save it, I can read that I can use the plist and SQLite, so my quest is if I have about 16-20 names and should only used em on the device where the data have been saved and where I can edit and deleted them, what would you recommend to use as a data source ! or would you recommend another approach ?
Im coding in Xcode atm.
I would suggest you to use CoreDataModel. CoreDataModel is a data storage model for Objective C. It's the way faster then SQL databases and similar to in terms of creating tables, making table connections. You can find more information in Ray Wenderlich's Tutorial.
Since you data model would include many attributes (name, age, etc.), or even relational models I would not suggest using plists in your case.
you can use NSUserDefaults if the data you want to store is very small.
Lets say you have an array of users
NSArray *users = ,,,,,;
to save it
[[NSUSerDefaults standardUserDefaults] setObject:users forKey:#"myKey"];
For later to get users data,
NSARray *users = [[NSUSerDefaults standardUserDefaults] objectForKey:#"myKey"];

Best way to store user information for my iOS app

What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.

Store an array of UIViews in NSUserDefaults

I'm trying to add an array of uiviews to NSDefault but it doesn't seem to be keep the array. Does any one know why? I also tried to store each view in nsvalue before storing it in nsdefault which still didn't work.
NSArray *arr = [[NSArray alloc] initWithObjects:[NSValue valueWithNonretainedObject:myView], nil]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:arr forKey:#"myKey"];
NSArray *resultArray = [defaults objectForKey:#"myKey"];
and resultArray is nil!
Thanks
the reason why I'm trying to do this is because these are the header views of my uitableview. Since it takes time to create them I wanted to create them only once and store them for future access.
From the docs for NSUserDefaults:
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide.
If you want to put a UIView (why?) in NSUserDefaults, you need to archive it first into an NSData object.
But you need to ask yourself why you want to put a view in NSUserDefaults. You should only be putting bits of data in NSUserDefaults. Views display data. It's easy to redisplay a view once you have the data back. Consider just putting some needed data in NSUserDefaults.
Are you sure you want to do that? It is definitely better to store an array of models to the data base or some file and recreate views from them when needed.
A ha! You are not the first person to face this issue. I've not had this type of issue myself but, in the link below, is a blog with code that allows you to cache and re-use your views. Then you would only need to re-create the views when you launch. Example code:
Cache UIViews for re-use in tableview

Resources