Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to save data to iOS?

Tags:

xcode

ios

save

ios5

In my application (iOS 5) I want to save data - I want to save debts. So its:

  • plus or minus money
  • the amount of money
  • and the name who has the debts (or the name where you have the debts)

But I don't how to save the data (NSUserdefaults,Core data, SQLLite)

Maybe you can tell me the best way to save them?

like image 345
H3rrVorr4g3nd Avatar asked Jun 17 '12 15:06

H3rrVorr4g3nd


People also ask

What is the best way to save data on iPhone?

With iOS 13 and later, you can turn on Low Data Mode to restrict background network use and save cellular and Wi-Fi usage. You might want to use Low Data Mode if your cellular or internet plan limits your data usage, or if you're in an area with slow data speeds.

What is the best way to save your data?

The easiest way to save data on your Android device is to connect to Wi-Fi as often as possible to avoid using unnecessary data. If you're doing something on your Android device that requires a lot of data, like streaming videos or performing app updates, connected to Wi-Fi will save you a lot of data in the long run.

How do I backup my mobile data on iOS?

Note: On models that support 5G, your carrier may give you the option to back up iPhone using your cellular network. Go to Settings > [your name] > iCloud > iCloud Backup, then turn on or off Backup Over Cellular. To perform a manual backup, tap Back Up Now.

How do I transfer data from iPhone to iPhone without losing everything?

Quick Start: Use your iPhone or iPad to automatically set up a new device. iCloud: Transfer your data and purchased content to your new device from your previous device's iCloud backup. iTunes or Finder: Transfer your data and purchased content to your new device from a backup you made with iTunes or Finder.


2 Answers

The easiest way to store small amount of data on your device is to use NSUserDefaults. But only property lists could be saved in this way. A property list is a combination of objects of 6 types, NSNumber, NSString, NSArray, NSDictionary, NSDate, NSData. In your case it's easy to do. For example, to save a new debt record you can use following method:

#define DEBTS_LIST_KEY @"listOfAllDebts"
#define DEBTOR_NAME_KEY @"debtorName"
#define DEBT_AMOUNT_KEY @"amountOfDebt"

-(void) saveDebt:(CGFloat) debtAmount forName:(NSString *) debtorName
{
    // pointer to standart user defaults
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    // the mutalbe array of all debts
    NSMutableArray * alldebtRecords = [[defaults objectForKey:DEBTS_LIST_KEY] mutableCopy];
    // create new record
    // to save CGFloat you need to wrap it into NSNumber
    NSNumber * amount = [NSNumber numberWithFloat:debtAmount];

    NSDictionary * newRecord = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:amount,debtorName, nil] forKeys:[NSArray arrayWithObjects:DEBT_AMOUNT_KEY, DEBTOR_NAME_KEY, nil]];
    [alldebtRecords addObject:newRecord];
    [defaults setObject:alldebtRecords forKey:DEBTS_LIST_KEY];
    // do not forget to save changes
    [defaults synchronize];
}

To readList of debts you have read something similar.

But I recommend you to use core data. It's more flexible and you won't have to write all this code to manage your data (to edit existed records, or to delete them). You will be able to extend your model much easier, for example, when you want to save the date of the debt. This is the link to a good tutorial

like image 123
Yarlik Avatar answered Oct 16 '22 11:10

Yarlik


If the quantity of records is user-defined, and will grow with app use, I suggest Core Data, which can be backed by SQLite. If you are working in a modern Xcode (i.e. Xcode 4), creating models is easy and graphical. If you have ever worked with ORM frameworks before, the interface for querying, etc. should be easy to grasp.

Search around for some tutorials, but be specific about finding tutorials that match your version of Xcode, as Core Data development has been changing a lot lately.

like image 3
Chris Trahey Avatar answered Oct 16 '22 09:10

Chris Trahey