Swift Language IOS8 Development battle Note Core Data2

Source: Internet
Author: User

The new Year is coming, I wish you all a happy goat, beaming. In the previous session we briefly introduced some basic knowledge of coredata, which we programmed to achieve the use of coredata. Some of the variables created in this statement are in contrast to the important variables in the coredata mentioned in the previous sentence, and in Appdelegate, add the following code:

Lazy var applicationdocumentsdirectory:nsurl = {Let        urls = Nsfilemanager.defaultmanager (). Urlsfordirectory (. Documentdirectory, Indomains:. Userdomainmask)        return urls[urls.count-1] as Nsurl        } ()//Official comparison of recommended wording

Similar to most of the database accesses we contacted before, this is the URL set by a closure.

  Lazy var Managedobjectmodel:nsmanagedobjectmodel = {Let        modelurl = Nsbundle.mainbundle (). Urlforresource ("Model", Withextension: "MOMD")        return Nsmanagedobjectmodel (contentsofurl:modelurl!)!        } ()//load Model

Remember the model that we defined in CoreData, which is loaded with this code.

Lazy var persistenetstorecoordinator:nspersistentstorecoordinator = {var coordinator:nspersistentstorecoordinator ? = Nspersistentstorecoordinator (managedObjectModel:self.managedObjectModel) Let URL = Self.applicationdocumentsdire Ctory. Urlbyappendingpathcomponent ("Data.sqlite") var error:nserror? = nil var failurestring = "Three was a" error when creating or loading the application ' s saved data "if coor Dinator?.            Addpersistentstorewithtype (Nssqlitestoretype, Configuration:nil, Url:url, Options:nil, error: &error) = = Nil { Coordinator = nil Let dict = Nsmutabledictionary () dict[nslocalizeddescriptionkey] = "Failed To initialize the application ' s save data "dict[nslocalizedfailurereasonerrorkey" = failurestring di            Ct[nsunderlyingerrorkey] = Error Error = nserror (domain: "your_error_domain", code:9999, Userinfo:dict)   NSLog ("Unsolved error\ (Error), \ (Error.userinfo)")         Abort ()//error directly exit} return coordinator!//no error return}//Persistent 

The above code defines the persistence of memory and how errors are handled

   Lazy var managedobjectcontext:nsmanagedobjectcontext = {Let        coordinator = Self.persistenetstorecoordinator        var managedobjectcontext = Nsmanagedobjectcontext ()        managedobjectcontext.persistentstorecoordinator = Coordinator        return Managedobjectcontext                } ()//context
After the variable definition is sufficient, we define the method:

    Func Savecontext () {        if let MoC = self.managedobjectcontext {            var error:nserror? = Nil            if Moc.haschanges & &!moc.save (&error) {             NSLog ("Unsolved error\ (Error), \ (error.userinfo)")                abort ()            }        }        }

As I said in the previous statement, if the Save method is not called, then our operations on CoreData will be in memory, not persisted. After defining the method, our preparation for CoreData has been completed. Finally, CoreData is not a database, it's just a framework that helps us with database operations, and it doesn't care where we store the data. In addition, we only create a model that is not enough and need to create an associated file as shown:

All the way down, the following actions are automatically associated, and the resulting files are as follows:




Swift Language IOS8 Development battle Note Core Data2

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.