ios开发进阶程序员首页投稿(暂停使用,暂停投稿)

开始用Swift开发iOS 10 - 17 使用Core Dat

2017-07-26  本文已影响403人  Andy_Ron

上一篇 开始用Swift开发iOS 10 - 16 介绍静态Table Views,UIImagePickerController和NSLayoutConstraint 中添加新建restaurant页面,但最后数据并没有保存下来,这一篇使用Core Data方式来持久化保存数据。

数据持久化一般是指数据库保存。在Web开发中,常用Oracle或MySQL等关系数据库来保存数据,通过SQL语句查询。在iOS中对应的数据库是SQLite。Core Data不是数据库,它是让开发者通过面向对象方式与数据库进行交互的库。

使用Core Data的例子

新建一个使用Core Data的项目,在AppDelegate类中会比平常多了一个变量和一方法,另外还多了一个文件CoreDataDemo.xcdatamodeld

    lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
        */
        let container = NSPersistentContainer(name: "CoreDataDemo")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                 
                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    // MARK: - Core Data Saving support

    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

向项目中添加Data Model

选中特定属性后可在右侧检查器中设置相关特性,比如是否强制需要。


创建Managed Objects

Core Data框架中的 Managed ObjectsEntity之间的关系,有点像代码中 接口变量UI objects之间的关系。xcode可自动生成Managed Objects

cell.thumbnailImageView.image = UIImage(data: restaurants[indexPath.row].image
as! Data)

if let imageToShare = UIImage(data: self.restaurants[indexPath.row].image as!
Data) {
由于`RestaurantMO`的属性值是可选值,使用时需要解包:
let defaultText = "Just checking in at " + self.restaurants[indexPath.row].name!
```

placemarks, error in
```

现在能成功运行,发现是没有数据的。

保存新数据到数据库

// MARK: - Core Data stack
    
    lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "FoodPin")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                
                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
    
    // MARK: - Core Data Saving support
    
    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
    // 1
    if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
        restaurant = RestaurantMO(context: appDelegate.persistentContainer.viewContext)
        restaurant.name = nameTextField.text
        restaurant.type = typeTextField.text
        restaurant.location = locationTextField.text
        restaurant.isVisited = isVisited
        
        if let restaurantImage = photoImageView.image {
            // 2
            if let imageData = UIImagePNGRepresentation(restaurantImage) {
                restaurant.image = NSData(data: imageData)
            }
        }
        
        print("Saving data to context ...")
        appDelegate.saveContext()
    }

运行,添加新的restaurant后并没有在Food Pin中显示,实际已经添加到数据库中,在RestaurantTableViewController里没有向数据库获取。

通过CoreData获取数据

        // 1   
        let fetchRequest: NSFetchRequest<RestaurantMO> = RestaurantMO.fetchRequest()
        // 2
        let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
            let context = appDelegate.persistentContainer.viewContext
            fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
            fetchResultController.delegate = self
        }
        
        do {
            // 3
            try fetchResultController.performFetch()
            if let fetchedObjects = fetchResultController.fetchedObjects {
                // 4
                restaurants = fetchedObjects
            }
        } catch {
            print(error)
        }
    func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        tableView.beginUpdates()
    }
    
    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type:
        NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
        switch type {
        case .insert:
            if let newIndexPath = newIndexPath {
                tableView.insertRows(at: [newIndexPath], with: .fade)
            }
        case .delete:
            if let indexPath = indexPath {
                tableView.deleteRows(at: [indexPath], with: .fade)
            }
        case .update:
            if let indexPath = indexPath {
                tableView.reloadRows(at: [indexPath], with: .fade)
            } default:
                tableView.reloadData()
        }
        if let fetchedObjects = controller.fetchedObjects {
            restaurants = fetchedObjects as! [RestaurantMO]
        }
    }
    
    func controllerDidChangeContent(_ controller:
        NSFetchedResultsController<NSFetchRequestResult>) {
        tableView.endUpdates()
    }

现在运行程序,添加新的Restaurant就能同步显示了。

通过CoreData删除数据

更新RestaurantTableViewControllertableView(_:editActionsForRowAt:_)方法中的 deleteAction

let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: {
            (action, indexPath) -> Void in
            
            if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
                let context = appDelegate.persistentContainer.viewContext
                let restaurantToDelete = self.fetchResultController.object(at: indexPath)
                context.delete(restaurantToDelete)
                
                appDelegate.saveContext()
            }
        })

现在删除一项后,重新启动后,数据消失。

更新数据

更新RestaurantDetailViewController的中的ActionratingButtonTapped:

    @IBAction func ratingButtonTapped(segue: UIStoryboardSegue) {
        if let rating = segue.identifier {
            restaurant.isVisited = true
            
            switch rating {
            case "great":
                restaurant.rating = "Absolutely love it! Must try."
            case "good":
                restaurant.rating = "Pretty good."
            case "dislike":
                restaurant.rating = "I don't like it."
            default:
                break
            }
        }
        
        if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
            appDelegate.saveContext()
        }
        
        tableView.reloadData()
    }

现在评价一项后,重新启动后评价就会保留。

Exercise:添加新字段

之前新建Restaurant页面没有Phone字段,现在添加

代码

Beginning-iOS-Programming-with-Swift

说明

此文是学习appcode网站出的一本书 《Beginning iOS 10 Programming with Swift》 的一篇记录

系列文章目录

上一篇 下一篇

猜你喜欢

热点阅读