swift语言coredata第三方库MagicalRecord最简洁教程

cocoapods
MagicalRecord

pod ‘MagicalRecord’安装MagicalRecord

/*************************以下为教程****************************/

AppDelegate.swift
import MagicalRecord

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        MagicalRecord.setupCoreDataStack()//只加这句话初始化
        return true
    }

ViewController.swift
import MagicalRecord

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        /*创建*/
        let admin = Admin.MR_createEntity()//创建
        admin?.token = "1234"//编辑
        print("创建")
        NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()//保存
        /*创建结束*/
        /*查询*/
        let admins = Admin.MR_findAll()//查询
        for a in admins as! [Admin]{
            print(a.token)
            a.token = "liuman"//编辑
            //a.MR_deleteEntity()//删除
        }
        print("条件查询token值为liuman的")
        let liuman = Admin.MR_findFirstByAttribute("token", withValue: "liuman")
        print(liuman!.token)
        /*查询结束*/
        //Admin.MR_truncateAll()//删除所有
        NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()//创建编辑删除之后都要保存,原save方法,被取代了。
        //MagicalRecord.cleanUp()//app退出前操作,关闭coredata
    }

coredata:CRED

import CoreData

//添加数据
    func saveCoreDate(){
        //加载AppDelegate
        let app = UIApplication.sharedApplication().delegate as! AppDelegate
        //获取管理的上下文
        let context = app.managedObjectContext
        var error:NSError?
        //创建一个实例并给属性赋值
        let admin = NSEntityDescription.insertNewObjectForEntityForName("Admin", inManagedObjectContext: context) as! Admin
        admin.token = "1"
        //保存数据
        do {
            try context.save()
            print("success!")
        }catch let error{
            print("context can't save!, Error:\(error)")
        }
    }
    func fetchCoreData (){
        //加载AppDelegate
        let app = UIApplication.sharedApplication().delegate as! AppDelegate
        //获取管理的上下文
        let context = app.managedObjectContext
        // 声明数据请求实体
        let fetchRequest = NSFetchRequest(entityName: "Admin")
        let predicate = NSPredicate(format:"token='1'")
        //fetchRequest.predicate=predicate//设置查询条件按照id查找不设置查询条件,则默认全部查找
        //执行查询操作
        do {
            let list =
                try context.executeFetchRequest(fetchRequest) as! [NSManagedObject]
            print("打印查询结果")
            for i in list as! [Admin] {
                print(i)
                //修改操作:将查询到的结果修改后,再调用context.save()保存即可
                i.token = "1"
                //删除操作:将查询到的额结果删除后,再调用context.save()保存即可
                context.deleteObject(i)
            }
        }catch let error{
            print("context can't fetch!, Error:\(error)")
        }
        do {
            try context.save()
            print("success!")
        }catch let error{
            print("context can't save!, Error:\(error)")
        }
        
    }