How to use CoreData (Jane)

Source: Internet
Author: User

There are only four ways to know iOS storage, often nsuserdefault and coredata. The former uses self-needless to say. Here is only a summary of the simple usage of coredata. For beginners Reference.
This article will demonstrate the basic use of CoreData by a small demo: the storage of data, and the behavior of adding and deleting changes.
First we want to determine the relationship of two objects
A family family has multiple members member, and a member belongs to only one family.
1. We first create a project, tick the use Core data option

2. In the. xcdatamodel file, create two entities, family and member

3. Establish a connection between two entities, type represents a relationship between two entities, to many indicates that a family can correspond to multiple member objects, and the Members property is a collection of these member objects;

Another important point here is the four options for delete rule,deleterule:
Deny can deny delete requests
Nullify resets the inverse relationship before the object is deleted, and when the object family is deleted, his subordinate member member still exists independently in the database, only member the corresponding family in the database is null.
Cascade deletes the object and all its relationships (cascade Delete), and when a family object is deleted, all member objects associated with him are also deleted
No Action will guarantee that the object to which a relationship points is not affected, even if the object points to the item that is about to be deleted

4. By default, entities taken with core data are of type Nsmanagedobject and can access data using key-value pairs. However, in general, the entity, on the basis of access to data, sometimes need to add some business methods to accomplish some other tasks, then you must create a subclass of Nsmanagedobject,




5. In database processing, the basic operation of objects and additions and deletions are the undisputed basis:

+(void) Addnewfamilywithname: (NSString*) name Address: (NSString*) Address success: (void(^) ()) Success failure: (void(^) (Nserror*error)) failure{appdelegate *appdelegate = [uiapplicationSharedapplication]. Delegate; Nsmanagedobjectcontext *context = appdelegate. Managedobjectcontext; Nsmanagedobject *family = [Nsentitydescription insertnewobjectforentityforname:@"Family"Inmanagedobjectcontext:context]; [Family Setvalue:name forkey:@"Name"]; [Family Setvalue:address forkey:@"Address"];Nserror*error =Nil;BOOLIssuccess = [Context save:&error];if(!issuccess) {if(failure)        {failure (error); }    }Else{if(success)        {success (); }    }}+(void) deletefamily: (Family *) Family success: (void(^) ()) Success failure: (void(^) (Nserror*error)) failure{appdelegate *appdelegate = [uiapplicationSharedapplication]. Delegate; Nsmanagedobjectcontext *context = appdelegate. Managedobjectcontext; [Context deleteobject:family];Nserror*error =Nil;BOOLIssuccess = [Context save:&error];if(!issuccess) {if(failure)        {failure (error); }    }Else{if(success)        {success (); }    }}+(Nsarray*) fetchfamilylists{appdelegate *appdelegate = [uiapplicationSharedapplication]. Delegate; Nsmanagedobjectcontext *context = appdelegate. Managedobjectcontext;//initialization of a query requestNsfetchrequest *request = [[Nsfetchrequest alloc] init];//Set the entity to queryRequest. Entity= [Nsentitydescription entityforname:@"Family"Inmanagedobjectcontext:context];//Set sort (by age descending)    //Nssortdescriptor *sort = [nssortdescriptor sortdescriptorwithkey:@ "age" ascending:no];    //request.sortdescriptors = [Nsarray arraywithobject:sort];    //Set conditional filtering (search for records containing the string "M" in name, note: When setting conditional filtering, the% in database SQL statements is replaced by *, so%m% should be written *m*, more filtering methods, can see the next article reproduced)    //Nspredicate *predicate = [nspredicate predicatewithformat:@ "name like%@", @ "*m*"];    //request.predicate = predicate;    //Execution Request    Nserror*error =Nil;Nsarray*OBJS = [Context executefetchrequest:request error:&error];if(Error) {        [NSExceptionraise:@"Query Error"format:@"%@", [Error localizeddescription]]; }//Traverse data     for(Nsmanagedobject *obj in Objs) {NSLog(@"name=%@", [obj valueforkey:@"Name"]); }returnOBJS;}

OK, that's the basic.
Enclosed This document demo address (OC): Https://github.com/SunflowerGJ/CoreDataTest.git
Corresponds to swift similar to Demo:https://github.com/tyronezhang/swiftcoredatademo

How to use CoreData (Jane)

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.