"We all love Paul Hegarty." Stanford IOS8 public class personal note persistence persistent

Source: Internet
Author: User

This session describes four ways to persist data in iOS:
Archiving
Sqlite
File System
Core Data
Before we use the result nsusrdefaults, which is a persistence technique for small data volumes, the four ways to do this are for large data volumes.
1.Archiving
Archiving is a way to store objects on a hard disk, and the stored objects do not require a directory of all properties. As long as an object implements the two methods of archiving, all the branch properties of this object can be stored using archiving:

funcencodeWithCoder(encoder:NSCoder)init(coder:NSCoder)

Encoderwithcoder This method is to write an object in the form of a dictionary and then encode it. When these objects need to be read, init initializes them. Storyboard is stored using this technique, and all uiview, Uiviewcontroller are encoded, because they all adhere to the Nscoding protocol. For example, when a uiview is saved, a chart of all the objects it contains will be saved. So when we use init to create a uiviewcontroller, we need an init coder property. This mechanism is ideal for storyboard, but it is not recommended for us.
2.SQLite
Sqllte is a mobile SQL database, many of the APIs that operate SQL database are C-style and not suitable for swift programmers.
3.File System
A UNIX kernel-based file system is available as long as it is a device that uses iOS. Saving data using the file system is stored in this filesystem, and you need to do this in four steps:
(1) Specify root path, such as documents directory, caches directory
(2) Add the file directory to the URL, using the Append method
(3) Then you can read and write files based on this directory.
(4) Managing the file system, using Nsfilemanager
The file system URLs are all from the "/." Initially, the file system opens up a separate storage space for each application, and this portion of the storage space between applications is isolated, which is called a "sandbox." Even in a sandbox, some parts are not allowed to write, such as where the application itself is stored. You can read all the sections in the sandbox.
As for why "sandbox" is used, the first is for security reasons. Next, when you delete an app, it will delete the sandbox together. Backup, too, backs up content in a specific sandbox, not all directories.
There are various bundles in the sandbox, in the form of catalogs.
Your app uses the application bundle directory, which stores all the app-related content, such as storyboard, pictures, and so on, which are not writable, and if you want to modify them you need to take them out and put them in application. Changes in directories other than the bundle directory
The Documents directory holds user data.
The Caches directory holds data that can be discarded.
So how do you get a path to access these directories? Using Nsfilemanager:

let fileManager = NSFileManager()

Nsfilemanager is thread-safe and you can only call this instance in the thread that created the Nsfilemanager instance. Defaultfilemanager is called in the main thread, otherwise it is a new instance, and creating an instance of Nsfilemanager is lightweight. Once you have a filemanager, you can access it. This method returns an array, because calling this API in a Mac may return many URLs, but on the iphone you can only get the user's profile, and we only need the first element in the array:

let urls:[NSURL] = fileManager.URLsForDirectory(NSSearchPathDirectory,inDomain:NSUserDomainMask)

The nssearchpathdirectory parameter selects the directory you want to retrieve, such as incoming nsdocumentsdirectory. You can write something new in these paths, using the Nsurl method:
Urlbyappendingpathcomponent (Nsurl) and
Urlbyappendingpathextension (Nsurl)
Sometimes when you get a URL and want to know if it's a URL, like a URL to a file, the system provides us with the appropriate method:
Isfilereferenceurl ()
There is also an important way to do this:

publicresourceValuesForKeysthrows -> [String : AnyObject]

Use this method to pass the key to the keys parameter, which you want to know, to query the last access time, size, and so on of the file, which is returned in the returned dictionary.
If you want to save a file to a URL, using NSData, sounds, images, and so on will usually be stored in nsdata format:

public func writetourl< Span class= "Hljs-params" > (Url:nsurl, atomically:bool) ->  Bool  

The

atomically parameter means to write a temporary file, then delete the old file, and then put the new file in and keep it atomic.
4.Core Data
If you know Core data, you'll learn more about how iOS works.
Core Data uses an object-oriented approach to accessing data in SQL instead of using SQL commands. When using core data, you need to pre-set the mappings for tables and objects in the database, each of the rows of the model in the database corresponds to each property in the object, and the property is created using the
@NSManaged Var, which means that you change the value in the database while changing the value of the property. You can create a model in a visual way.

Then get the handle (Controller) or hook for the database:
Uimanagedobjectcontext, this variable needs to be defined in appdelegate. Also select "Use Core Data" when creating the project. There's also a class uimanageddocument, which encapsulates a core data database. The coolest thing about uimanageddocument is that if you use it, the icloud section will be a lot easier. Using context, you can create objects in the database, use the Insertnewobjectforentityforname
of the Nsentitydescription class, and Executefetchrequest methods. Pass in the parameters of the Nsfetchrequest type, you can set the query request to query the data in the database, get the return value of Nsarray type, the member type is nsmanagedobject. This is what you can do with the database. Once you have these Nsmanagedobject objects, you can use both methods: SetValue and Valueforkey to manipulate the object, or to create a subclass of NSObject, usually with the same name as the entity in the database:

class Photo:NSObject {@NSManagedtitle:String}

Then use the set and get methods to manipulate the properties:

let photo = NSEntityDescription.insertNewObjectForEntityForName("Photo",in"My First Photo"

Core data is also good at working with table view, because you might need to use Table view to display if there is a lot of data in core. In iOS there is a class Nsfetchedresultscontroller class, you give it a query request, it will associate a tableview, this tableview and your database is real-time association, And this class will implement all the DataSource methods of TableView.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"We all love Paul Hegarty" Stanford IOS8 public class personal note persistence persistent

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.