Skip to content

Commit b5e0a42

Browse files
author
cyper
committed
v5.0: core data
1 parent fb7a79a commit b5e0a42

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ leading.isActive = true
334334

335335
### 关闭view controller两种办法
336336

337-
338337
```swift
339338
@IBAction func saveRestaurant() {
340339
if checkForm() {
@@ -345,6 +344,87 @@ performSegue(withIdentifier: "unwindToHomeScreen", sender: self)
345344
```
346345
因为我想沿用Cancel按钮使用的unwind segue,在用第2种方法的时候碰到了找不到unwind segue identifier的问题, 参见[SO]解决: 虽然unwind segue的Action segue是根据你所定义的func名字生成的, 但是identifier还是需要自己指定.
347346

347+
### CoreData Data Model
348+
新建一个Data Model: `FoodPinDemo.xcdatamodeld`, Entity改名为Restaurant, Class改名为RestaurantMO(这个Managed Object类编译项目会自动生成, project中看不到)
349+
350+
RestaurantMO所有属性都变成了optional, 并且image(binary)的类型为NSData.
351+
352+
主要变化
353+
354+
1. UIImage(named: restaurant.image) -> UIImage(data: restaurant.image as! Data)
355+
2. resturant.location -> restaurant.location!
356+
3. restaurant.rating -> restaurant.rating ?? ""
357+
358+
之前的Restaurant.swift废掉, 可以删除.
359+
360+
### CoreData CRUD
361+
findAll, 在viewWillAppear中加入:
362+
363+
```swift
364+
let appDelegate = UIApplication.shared.delegate as! AppDelegate
365+
let ctx = appDelegate.persistentContainer.viewContext
366+
let request: NSFetchRequest<RestaurantMO> = RestaurantMO.fetchRequest()
367+
let restaurants = try! ctx.fetch(request)
368+
```
369+
370+
简单封装的工具类CD(save传的参数没有用到, 只是为了好看!):
371+
372+
```swift
373+
class CD {
374+
375+
class var appDelegate: AppDelegate {
376+
return UIApplication.shared.delegate as! AppDelegate
377+
}
378+
class var ctx: NSManagedObjectContext {
379+
return appDelegate.persistentContainer.viewContext
380+
}
381+
382+
class func delete<T: NSManagedObject>(_ o: T) {
383+
ctx.delete(o)
384+
save(o)
385+
}
386+
387+
class func save(_ _: NSManagedObject) {
388+
appDelegate.saveContext()
389+
}
390+
391+
class func image2Data(image: UIImage?) -> NSData? {
392+
if let image = image {
393+
if let imageData = UIImagePNGRepresentation(image) {
394+
return NSData(data: imageData)
395+
}
396+
}
397+
398+
return nil
399+
}
400+
```
401+
这个类间歇性的报ambiguous use of x 的错误, 但是程序能正常运行, 怀疑是Xcode8.1的bug.
402+
403+
增删改查:
404+
405+
```swift
406+
// create (AddRestaurantController)
407+
let restaurant = RestaurantMO(context: CD.ctx)
408+
restaurant.name = name
409+
restaurant.type = type
410+
restaurant.image = CD.image2Data(image: photoImageView.image)
411+
CD.save(restaurant)
412+
413+
// delete (editActionsForRowAt)
414+
let restaurant = restaurants.remove(at: indexPath.row)
415+
tableView.deleteRows(at: [indexPath], with: .fade)
416+
CD.delete(restaurant)
417+
418+
// update (ratingButtonTapped)
419+
restaurant.isVisited = true
420+
CD.save(restaurant)
421+
422+
// find all (viewWillAppear)
423+
let request: NSFetchRequest<RestaurantMO> = RestaurantMO.fetchRequest()
424+
restaurants = try! CD.ctx.fetch(request)
425+
tableView.reloadData()
426+
```
427+
待研究: http://stackoverflow.com/questions/37810967/how-to-apply-the-type-to-a-nsfetchrequest-instance
348428

349429
### Keywords
350430

@@ -365,6 +445,7 @@ performSegue(withIdentifier: "unwindToHomeScreen", sender: self)
365445
### Omitted
366446
1. Swipe to hide
367447
2. MapKit: show image on callout bubble
448+
3. NSFetchedResultsController
368449

369450
### Xcode tricks
370451

0 commit comments

Comments
 (0)