你的位置:首页 > 软件开发 > 操作系统 > iOS10 CoreData新特性

iOS10 CoreData新特性

发布时间:2016-09-11 15:00:10
原文地址:Whats New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0翻译人:肖品,原创文章转载请著名出处。 Core Data在 macOS 10.12 , iOS 10.0, tv ...

原文地址:What's New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0

翻译人:肖品,原创文章转载请著名出处。

 

Core Data在 macOS 10.12 , iOS 10.0, tvOS 10.0和watchOS 3.0中的新特性

This document describes the new areas of functionality in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0. Please note that many Swift APIs are renamed in accordance with Swift 3 API design guidelines. Please refer to Swift Evolution document SE-0023, "API Design Guidelines."

这个文档描述了CoreData在 macOS 10.12, iOS 10.0,tvOS 10.0 和 watchOS 3.0的新功能。请注意有许多Swift的API 根据Swift 3 API 设计指南进行了重命名。请参考 Swift 进化文档 SE-0023, ”API 设计指南“

 

Concurrency changes and connection pooling

并发变化和连接池

The NSPersistentStoreCoordinator now maintains a small connection pool that allows concurrent database operations for NSSQLiteStoreType persistent stores. An NSManagedObjectContext (that is not nested) can now fetch and fault concurrently with other peer NSManagedObjectContext instances. For persistent stores using WAL journal_mode (the default in Core Data), the NSPersistentStoreCoordinator also supports 1 writer concurrently with multiple readers. For example, a UI context can fetch data concurrently with a single background context importing changes. Connection pooling uses less memory and generally out performs multiple separate NSPersistentStoreCoordinator instances to the same file.

NSPersistentStoreCoordinator(持久化协调者) 现在维持了一个小连接池,来NSSQLiteSToreType持久化进行并发数据操作。一个NSManagedObjectContext(对象上下文)和它对等的NSManagedObjectContext实例可以马上取数据和让并发失效。CoreData持久化默认使用的是WAL journal_mode, NSPersistentStoreCoordinator也支持1个写入者和多个读取者并发。举个例子:一个后台上下文在载入改变 ,同时一个界面上下文在读取数据。 对于同一个文件,连接池会使用较少的内存,多个NSPersistentStoreCoordinator实例对象通常都会分开执行。

This behavior is enabled for all existing Core Data clients. The default number of connections varies by platform but is greater than 1. You can adjust the behavior with the NSPersistentStoreConnectionPoolMaxSizeKey in your options dictionary when adding the store to the coordinator. NSPersistentStoreCoordinator briefly dispatches requests through its own queue, and custom code in blocks passed to NSPersistentStoreCoordinator.perform or NSPersistentStoreCoordinator.performAndWait will block the NSPersistentStoreCoordinator from routing future requests until they complete. Nested NSManagedObjectContext instances still serialize requests against their parent context as before.

在已经存在CoreData的客户端 这种行为是允许的。默认连接数量根据平台而异,但都是大于1。你在添加存储协调者的时候可以配置NSPersistentStoreConnectionPoolMaxSizeKey来调整行为。NSPersistentStoreCoordinator通过它的队列可以对所有请求短暂的分派,也可以通过NSPersistentStoreCoordinator.perform或者NSPersistentStoreCoordinator.performAndWait编写自定义代码,完成以后回调到指定的方法或Block代码块。嵌套的NSManagedObjectContext实例依旧针对父上下文保持连续请求。

 

NSPersistentContainer

持久容器

NSPersistentContainer is a new class that simplifies creating a new Core Data stack. It maintains references to your NSManagedObjectModel, NSPersistentStoreCoordinator, and other resources. NSPersistentContainer uses a new class NSPersistentStoreDescription to describe the configuration information to pass to NSPersistentStoreCoordinator when adding a persistent store. NSPersistentStoreDescription defaults to an NSSQLiteStoreType with automatic light weight migration enabled. The Xcode new project assistant for creating new iOS projects (but not macOS) that use Core Data now uses NSPersistentContainer in the AppDelegate. An example:

NSPersistentContainer是一个新类,它简化了创建一个心的CoreData堆。它维持了你项目中的NSManagedObjectModel ,NSPersistentStoreCoordinator 和其他资源的引用。在添加一个持久化存储的时候,NSPersistentContainer使用了一个新类NSPersistentStoreDescription来描述配置信息,并传到NSPersistentStoreCoordinator。NSPersistentStoreCoordinator默认可以自动对一个NSSQLiteStoreType进行迁移。在Xcode中新建iOS项目中,使用CoreData现在在AppDelegate中使用

NSPersistentContainer,示例如下:

let container = NSPersistentContainer(name: "myAppName")container.loadPersistentStores(completionHandler: { (storeDescription, error) in  if let error = error {    fatalError("Unresolved error \(error), \(error.userInfo)")  }  container.viewContext.perform({    // actions upon the NSMainQueueConcurrencyType NSManagedObjectContext for this container  })})

原标题:iOS10 CoreData新特性

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。