Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 13: Basic Data Persistence

491

That’s it! Build and run the app to make sure it works. The Core Data version of your application should behave exactly the same as the previous versions.

It may seem that Core Data entails a lot of work and, for a simple application like this, doesn’t offer much of an advantage. But in more complex applications, Core Data can substantially decrease the amount of time you spend designing and writing your data model.

Persistence Rewarded

You should now have a solid handle on four different ways of preserving your application data between sessions—five ways if you include the user defaults that you learned how to use in the previous chapter. We built an application that persisted data using property lists and modified the application to save its data using object archives. We then made a change and used the iOS’s built-in SQLite3 mechanism to save the application data. Finally, we rebuilt the same application using Core Data. These mechanisms are the basic building blocks for saving and loading data in almost all iOS applications.

Ready for more? In the next chapter, we’re going to continue talking about saving and loading data, and introduce you to iOS 5’s document system. This system not only provides a nice abstraction for dealing with saving and loading documents in files stored on your device, but it also lets you save your documents to Apple’s iCloud, one of the biggest new features in iOS 5.

www.it-ebooks.info

Chapter14

Hey! You! Get onto iCloud!

One of the biggest new features touted with the announcement of iOS 5 is Apple’s new iCloud service, which provides cloud storage services for iOS 5 devices, as well as for computers running Mac OS X and Microsoft Windows. Most iOS users will probably encounter the iCloud device backup option immediately when setting up a new device or upgrading an old device to iOS 5, and will quickly discover the advantages of automatic backup that doesn’t even require the use of a computer.

Computerless backup is a great feature, but it only scratches the surface of what iCloud can do. What may be even a bigger feature of iCloud is that it provides app developers with a mechanism for transparently saving data to Apple’s cloud servers with very little effort. You can make your apps save data to iCloud, and have that data automatically transfer to any other devices that are registered to the same iCloud user. Users may create a document on their iPad, and later view the same document on their iPhone without any intervening steps; the document just appears.

A system process takes care of making sure the user has a valid iCloud login and manages the file transfers, so you don’t need to worry about networks or authentication. Apart from a small amount of app configuration, just a few small changes to your methods for saving files and locating available files will get you well on your way to having an iCloud-backed app.

One key component of the iCloud filing system is the UIDocument class, which is also new to iOS 5. UIDocument takes a portion of the work out of creating a document-based app, by handling some of the common aspects of reading and writing files. That way, you can spend more of your time focusing on the unique features of your app, instead of building the same plumbing for every app you create.

Whether you’re using iCloud or not, UIDocument provides some powerful tools for managing document files in iOS. To demonstrate these features, the first portion of this chapter is dedicated to creating TinyPix, a simple document-based app that saves files to local storage. This is an approach that can work well for all kinds of iOS-based apps.

Later in this chapter, we’ll show you how to iCloud-enable TinyPix. For that to work, you’ll need to have one or more iCloud-connected iOS devices at hand. You’ll also need

D.Mark et al., Beginning iOS 5 Development

©Dave Mark, Jack Nutting, Jeff LaMarche 2011

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]