Working on an application that I want to check the plist store that the app will come shipped with and prepopulate core data with the objects from the plist. I think....I am looking for advice on this part of my application. As this is my first time coding and I have been learning Core Data/Persistence more recently I am not used to reading C++ or any of the code completely prior to Swift. I understand it somewhat but not enough to actually know the order of coding within swift.
That being said I created a plist file that contains an array of objects that each of those objects contains either strings or integers depending on the value that was necessary. Now in those objects I actually made a "serial" number as it were so that I could refer to specific objects easily or use it as a means to store that particular string and save only that so that it could be recalled from the plist. I have no idea whether I should actually upload all the objects to core data or just leave them in plist. Nor do I have any idea which way to begin this section of code.
From my basic understanding of plist and the fact that I saved the serial as a string I figured I could present the objects selected by storing the string alone to the core data model then have the core data model recall all objects from the plist that contain that serial and present it to the user. Basically any small sample coding in swift that can actually head me in the right direction would be a big help! Also is it faster to do it this way or would it be faster/better to upload the objects to core data on the first launch/updated lists? How would I go about this method if that was better? I spent the better half of the last few days trying to read through non swift code and haven't progressed enough I feel.
Sorry if this is n00bish I am self taught in swift and really trying to learn the best ways for all this as I have a lot of ideas that I wish to personally create/oversee!
Related
Hi I am a newbie to iPhone programming. I have been looking at some tutorials online for using SQLite on the iPhone.
From what Iv'e seen, one has to read the entire database in the app delegate method and then store the contents in an array. This is then used to populate a UITableView.
I was wondering how this can be efficient? I mean, if you are going to store the contents of your entire database in memory why have a database?
Is this standard practice?
Instead of this, if I use Core Data, I can set the paging size and it will optimize how much data is stored in memory and only read whats being shown within the visible cells.
Is there anyway, In can have the same functionality using SQLite? so its easy on memory. I mean my database has only 300 entries now, but what if in the future it has 3 million?
Can some one please assist me in understanding this....
Are there any tutorials for SQLite that show how to do it in an optimized way?
Thanks
I would suggest using Core Data. It's extremely powerful, better supported than a SQLite table, and not as complicated as it looks, it just uses a lot of "managed object" jargon which can be daunting at first. Check out the CoreDataHelper class, and a few tutorials. I was able to get Core Data working in a few days (in my first ever app) using the links in my comment
If you're familiar with SQL, basically Core Data works like this: the Persistent Store IS a SQLite table, but you never access it directly; the managedObjectContext is where you do all the data work in your program (and don't forget to save it to the persistent store!), while an Entity is a table, attributes of that entity are your columns, and a managed object is a row.
Further Reading:
Core Data Tutorial - YouTube
Core Data Tutorial: Getting Started | Ray Wenderlich
iPhone Core Data: Your First Steps
Here is some tutorial that may help you:
iPhone Programming Tutorial.
I recommend you consider using core data instead of sqlite.
I am relatively new to iOS and programming, and I made an app before, but it used a plist for storage, which I saved to the documents folder. Now, I am thinking about switching over to Core Data, but it looks a little complicated, and I'm not sure if it will work for what I want. I am going to have a bunch of data which I need to graph, so I'm not sure if Core Data is best for this, as it seems that I cannot create an array type in the .xcdatamodeld file. What are some other advantages of Core Data? Is it faster? Easier to use (once you set it up)?
Update: For anyone wondering, I finished the app, and it was totally worth it to learn how to use Core Data, and it was a lot less complicated that I originally thought. Doing it with plists would have been hell. The way they go about doing it seemed a little cryptic at first but if you just start using it you will get it. The relationships are really what is awesome about it.
A few advantages off the top of my head:
Much better memory management. With a plist you must load the entire thing into memory; with Core Data only the objects you're currently using need to be loaded. Also, once objects are loaded, they're normally placeholder "fault" objects whose property data doesn't load until you need it.
Related to the above, when you have changes, you can save only the changed objects, not the entire data set.
You can read/write your model objects directly instead of converting them to/from something like an NSDictionary.
Built-in sorting of objects when you fetch them from the data store.
Rich system of predicates for searching your data set for objects of interest.
Relationships between entities are handled directly, as properties on the related objects. With a plist you would need to do something like store an object ID for a relationship, and then look up the related object.
Optional automatic validation of property values.
Data models don't use arrays, but "to-many" relationships are modeled as sets.
It's a matter of what you're saving. For simple strings, arrays, dictionaries, it's fine to use a plist. For something more complicated (data, images, non-object information) or something with to-many relationships (think relationship between song to album, or photo to photographer), then something like a more robust solution might work better like SQLite.
CoreData is an objective-c-based wrapper around SQLite. If you think you might want to something more complicated, CoreData might be the way to go.
If you need a quick tutorial, I'd check out: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
This got me going and allowed me to learn the basics the workings of CoreData.
Good luck!
I would simply like to create a file that stays around after the program quits. I would like it to contain objects that I have initiated, is that possible? Or does it just have to have text in it? How can I access that file later? Also, is this done the same way on the Mac and the iPhone?
Take a look at Core Data or SQLite. Apple has some sample code for them, not sure about SQLite because they prefer you tou use core data.
I will direct you to this question. Adding core data to my iPhone app
Or use NSUserDefaults: Best way to save data on the iPhone
Yes! It's possible.
Derive your objects that you want to persist from NSCoding. On those objects, implement the initWithCoder and encodeWithCoder functions on those objects. In these functions, write all of your primitive types to the coder (or read them) using the functions here http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSCoder_Class/Reference/NSCoder.html
When you want to save your file, use an NSKeyedArchiver and pass in the object you want to archive along w/ the file name.
[NSKeyedArchiver archiveRootObject:root toFile:string];
More examples here https://stackoverflow.com/questions/2805026/iphone-problem-in-saving-file-using-nscoding
I have used SQLitePersistentObjects in the past to give my Objective-C class objects an ORM-esque feel and behaviour. The objects itself translate into corresponding tables in the database, which in this case is a simple SQLite database file.
Archives and Serializations Programming Guide might be interesting as well.
I am new to the iphone platform and am creating an app that retrievals a rss feed and displays it in a UITableView. I have gotten this working perfectly (or rather, the way I wanted). What I was thinking was I would store the current feed items on the phone so that it would initally load the old items it has stored while it got the new feed, then parse the new feed and add the new items and refresh the TableView. I was going to use Core Data to store it the old feed items because it would be a good way of learning Core Data and it would be an appropriate use of Core Data. However, I am having a difficult time learning how to use Core Data and connecting it with the Table/Array.
I have already googled and looked on stackoverflow for tutorials but have yet to find anything that explains it in a way I really understand. Any explanation of the overall steps that it takes to add Core Data to an existing app would be greatly appreciated. Full-blown detail are not necessary (but would also be useful). I'm just not very experienced with SQL or storing of data in such a manner and am having trouble wrapping my head around how the whole concept of Core Data works and how it connects to everything.
Also, any better method of doing what I'm trying to accomplish would also be appreciated.
There exist Xcode templates for Core Data-based applications; these are a great start to getting Core Data off the ground. However, it sounds like you want to integrate Core Data into your existing app, so you'll need to...
Add three main Core Data objects: the managed object context (MOC), the managed object model (MOM), and the persistent store coordinator (PSC). These need to be accessible wherever you want Core Data available, so either in your app delegate or, more preferably, in the controller or data source for your table view.
Create a MOM in Xcode. This will be a file of type .xcdatamodel, and it's an object graph that defines all the Core Data entities you want in your app.
Use NSFetchedResultsController (as suggested by Louis Gerbarg) to get data out of Core Data and display it into your table view.
Add code in your existing RSS-fetching-and-parsing routines to store new Core Data objects back into the store, when appropriate.
A good way to start is just to create a new Core Data application and play around with it a bit; you can also look at Apple's fantastic resources on the subject, like the Core Data Programming Guide and the sample apps Recipes and Locations. (Developer registration may be required.)
One last thing to note is that for the most part, a lot of the Core Data code you need to add can be ripped straight out of one of the Xcode template apps and pasted into your program (this holds especially true for the accessors for the three Core Data objects you need). Be careful not to use code you don't understand, though.
If you are using CoreData to populate a UITableView you really want to use NSFetchedResultsController as opposed to trying to populate and sync array yourself. The documentation for NSFetchedResultsController includes links to several CoreData tutorials, including onces that populate table views.
NSFetchedResultsController is still a bit buggy and requires fragile workarounds. I would start with the simpler iPhone Core Data "location" tutorial before moving on to the Books tutorial.
Also, any better method of doing what I'm trying to accomplish would also be appreciate
yes, it sounds like Core Data might be overkill for your application. Assuming your feed items are stored in a collection object you can easily use OSX's built in serializaition.
Been noted in other Stack-Overflow posts, but I can highly recommend the Prag Prog book "Core Data: Apple's API for Persisting Data on Mac OS X" - most is also relevant to iPhone Core Data apps; there is a whole chapter on creating an iPhone app too.
http://pragprog.com/titles/mzcd/core-data
Okay this is a fairly broad question. This is my first App and I'm not sure the best way to go about this. The app is on the IPHONE.
I have a 'Restaurant' class. The restaurant has many different attributes and opening times.
I currently store a restaurant in an instance of nsdata (it complies to NSCoding) LOcal storage is easy and I just use nsdata.
I have just built in an import/export function. I want these two methods to "post" a restaurant to the web and "get" a restaurant from the web. I know I can do this with NSUrlConnection and I have it up and working.
However I want to be cleverer about it. For instance what if I want to take my online list of restaurants that have been submitted and build a web interface that can also interact with the data?
This is what I am thinking so far:
Create a 'toString' method that will convert my class into a textual representation. Then store the string on a server. This will replace storing nsdata online. Does this sound appropriate?
However, I want to be able to query the amount of restaurants and have a bit more control over my online 'database'. Is XML the answer? I was about the start looking into Amazon Web Services and learn either Json or Rest.
Any bits of advice?
Thanks
Dan
I am not very familiar with iPhone development yet, but if there are reasonably easy way to consume web services I would recommend using that with typed parameters. This would make these services more "usable" from other systems too, they would not need to know how to serialize/pack objects to a string but only how to call a function: storeRestaurang(string name, int rating...) etc.
This is an article I will check out myself since I hope to get into iPhone development a bit more: http://icodeblog.com/2008/11/03/iphone-programming-tutorial-intro-to-soap-web-services/
Looks like you want to use a plist.
Any of the cocoa collection classes can be written out to a plist, which is a breed of xml file. The sweet part is if you use standard cocoa value classes, you can write the collection out, and read it back in later.
NSString, NSNumber, NSDate, NSData, NSAray, NSDictionary objects can be put into a collection and stored to a plist directly. Other classes will have to be serialized into NSData then written to disk as a plist. For truly custom data, an NSKeyedArchiver is probably what you want.
For simple strings and numbers data, pack it all into an NSDictionary and then write it to a plist. Now look around on your mac and you'll see just how popular plists are.
NSDictionary API - Storing Dictionaries
Property List Programming Guide
Archives and Serializations Programming Guide for Cocoa
Learn about REST, it's the easiest and cleanest way to provide a web-based API. Some frameworks, like Ruby on Rails, give you a REST interface right out of the box.
Then, you'll need to find a way to convert your objects on the iPhone into a REST-friendly format. XML and JSON are both options, but unfortunately I haven't seen any code for converting to/from XML or JSON that I can really recommend, since I haven't tried any just yet. But, there are some projects that might lead you in the right direction. One of the most promising-looking is this:
http://github.com/yfactorial/objectiveresource/tree/master