最近一段时间都在找工作,对于 iOS 方面知识的学习有些懈怠,有什么好书麻烦推荐给我呀~
Chapter 1
The NSPersistentContainer consists of a set of objects that facilitate saving and retrieving information from Core Data.
register(_:forCellReuseIdentifier:) guarantees your table view will
return a cell of the correct type when the Cell reuseIdentifier is provided to the dequeue method.
Core Data uses a SQLite database as the persistent store, so you can think of the Data Model as the database schema.
An entity is a class definition in Core Data. The classic example is an Employee ora Company. In a relational database, an entity corresponds to a table.
An attribute is a piece of information attached to a particular entity. For example, an Employee entity could have attributes for the employee’s name,position and salary. In a database, an attribute corresponds to a particular fieldin a table.
A relationship is a link between multiple entities. In Core Data, relationships between two entities are called to-one relationships, while those between one and many entities are called to-many relationships.
NSManagedObject doesn’t know about the name attribute you defined in your Data Model, so there’s no way of accessing it directly with a property. The only way Core Data provides to read the value is key-value coding, commonly referred to as KVC.
Like save(), fetch(_:) can also throw an error so you have to use it within a do block. If an error occurred during the fetch, you can inspect the error inside the catch block and respond appropriately.
Chapter 2 : NSManagedObject Subclasses
An attribute’s data type determines what kind of data you can store in it and how much space it will occupy on disk.
The number of bits reflects how much space an integer takes up on disk as well as how many values it can represent, or its range. Here are the ranges for the three types of integers:
Range for 16-bit integer: -32768 to 32767
Range for 32-bit integer: –2147483648 to 2147483647
Range for 64-bit integer: –9223372036854775808 to 9223372036854775807
When you enable Allows External Storage, Core Data heuristically decides on a per-value basis if it should save the data directly in the database or store a URI that points to a separate file.
You can save any data type to Core Data (even ones you define) using the Transformable type as long as your type conforms to the NSCoding protocol.
用一个 model 类替换用 kvc 的方式取值 : The best alternative to key-value coding is to create NSManagedObject subclasses for each entity in your data model.
context:
var managedContext: NSManagedObjectContext!
managedContext = appDelegate.persistentContainer.viewContext
增:
let entity = NSEntityDescription.entity(forEntityName: "Bowtie", in: managedContext)!
let bowtie = Bowtie(entity: entity, insertInto: managedContext)
//给 bowtie 赋值即可
查:
let selectedValue = "R"
let request = NSFetchRequest<Bowtie>(entityName: "Bowtie")
request.predicate = NSPredicate(format: "searchKey == %@", selectedValue!)
// 通过查找 entity bowtie 的属性之一 searchKey 来找到实体
do {
let results = try managedContext.fetch(request)
currentBowtie = results.first
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
改:
currentBowtie.rating = rating
try managedContext.save()
删:
managedContext.delete(currentBowtie)
try managedContext.save()
Chapter 3 : The Core Data Stack
The stack is made up for four Core Data classes:
- NSManagedObjectModel 代表 model 文件,被管理的数据模型,可以添加实体及实体的属性,若新建的项目带CoreData,即为XXX.xcdatamodeld
- NSPersistentStore 读写数据时选取的存储方法:
- NSQLiteStoreType、NSXMLStoreType、NSBinaryStoreType、NSInMemoryStoreType
- NSPersistentStoreCoordinator
- bridge between the managed object model and the persistent store 数据库的连接器,设置数据存储的名字,位置,存储方式等
- NSManagedObjectContext 操纵数据库实体时的工作板,所有操作必须调用其 save 方法才能在数据库上改变,负责应用与数据库之间的交互,增删改查基本操作都要用到
- NSPersistentContainer 将四个部分包含起来
- NSFetchRequest 获取数据时的请求
- NSEntityDescription 描述实体
Chapter 4 : Intermediate Fetching
In case you didn't know, NSFetchRequest is a generic type. If youinspect NSFetchRequest's initializer, you'll notice it takes in type as a parameter<ResultType : NSFetchRequestResult>.
ResultType specifies the type of objects you expect as a result of the fetchrequest. For example, if you're expecting an array of Venue objects, the resultof the fetch request is now going to be [Venue] instead of [AnyObject]. This ishelpful because you don't have to cast down to [Venue] anymore.
NSFetchRequest has a property named resultType. Sofar, you’ve only used the default value, NSManagedObjectResultType. Here are all thepossible values for a fetch request’s resultType:
• .managedObjectResultType: Returns managed objects (default value).
• .countResultType: Returns the count of the objects matching the fetch request.
• .dictionaryResultType: This is a catch-all return type for returning the resultsof different calculations.
• .managedObjectIDResultType: Returns unique identifiers instead of full-fledged managed objects.
To create an NSAsynchronousFetchRequest you need two things : a regular
NSFetchRequest and a completion handler. Your fetched venues are contained in
NSAsynchronousFetchResult’s finalResult property. Within the completion
handler, you update the venues property and reload the table view.
Chapter 5 : NSFetchedResultsController
var fetchedResultsController: NSFetchedResultsController<Team>!
let fetchRequest: NSFetchRequest<Team> = Team.fetchRequest()
let zoneSort = NSSortDescriptor(key: #keyPath(Team.qualifyingZone), ascending: true)
let scoreSort = NSSortDescriptor(key: #keyPath(Team.wins), ascending: true)
let nameSort = NSSortDescriptor(key: #keyPath(Team.teamName), ascending: true)
fetchRequest.sortDescriptors = [zoneSort, scoreSort, nameSort]
fetchedResultsController =
NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: coreDataStack.managedContext,
sectionNameKeyPath: #keyPath(Team.qualifyingZone),
cacheName: "worldCup")
fetchedResultsController.delegate = self
func numberOfSections(in tableView: UITableView) -> Int {
guard let sections = fetchedResultsController.sections else {
return 0
}
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sectionInfo = fetchedResultsController.sections?[section] else {
return 0
}
return sectionInfo.numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: teamCellIdentifier, for: indexPath)
let team = fetchedResultsController.object(at: indexPath)
cell.flagImageView.image = UIImage(named: team.imageName!)
cell.teamLabel.text = team.teamName
cell.scoreLabel.text = "Wins: \(team.wins)"
return cell
}