最近一段時間都在找工作浦译,對于 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
}