核心数据:执行提取后插入新行
Core Data: Inserting a new row after performing fetch
我无法解决这个 GCD 问题。
假设 table 视图中有一个 UITableViewCell。此单元格包含一些用户正在编辑的文本字段。用户完成编辑后,单元格会根据用户输入的内容在 Core Data 中创建一个新对象。此对象使用 insertNewObjectForEntityForName
.
插入
在此之后,我需要 performFetch
NSFetchedResultsController 并重新加载 table 视图。但是,我还想在 table 视图的末尾添加一个新单元格,以便用户可以将另一个项目添加到他们的列表中。
这就是我正在做的尝试:
dispatch_async(dispatch_get_main_queue(), {
do {
try self.fetchedResultsController.performFetch()
} catch {
fatalError("Fetch failed: \(error)")
}
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.fetchedResultsController.sections!.first!.numberOfObjects - 1, inSection: 0)], withRowAnimation: .Automatic)
self.tableView.reloadData()
})
添加了新单元格,但没有行动画。我认为我对 GCD 和 Core Data 有一些误解。感谢您的帮助。
需要使用第一个闭包作为异步处理运行的线程。该过程完成后,它将进入主线程上的第二个闭包,以对您的视图进行任何操作
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// process here
do {
try self.fetchedResultsController.performFetch()
} catch {
fatalError("Fetch failed: \(error)")
}
dispatch_async(dispatch_get_main_queue(), { Void in
// then refresh UI
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.fetchedResultsController.sections!.first!.numberOfObjects - 1, inSection: 0)], withRowAnimation: .Automatic)
self.tableView.reloadData()
})
}
查看本教程了解详情 https://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1
通过实施解决了这个问题:
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
在此函数内部使用 switch
语句可以更好地控制发生的每个更改:
switch type {
case NSFetchedResultsChangeType.Insert:
print("Insert")
case NSFetchedResultsChangeType.Delete:
print("Delete")
case NSFetchedResultsChangeType.Update:
print("Update")
case NSFetchedResultsChangeType.Move:
print("Move")
}
在此开关内,您可以使用 indexPath
删除单元格、添加单元格,随心所欲。只需使用 insertRowsAtIndexPaths
或其他相关函数。
我无法解决这个 GCD 问题。
假设 table 视图中有一个 UITableViewCell。此单元格包含一些用户正在编辑的文本字段。用户完成编辑后,单元格会根据用户输入的内容在 Core Data 中创建一个新对象。此对象使用 insertNewObjectForEntityForName
.
在此之后,我需要 performFetch
NSFetchedResultsController 并重新加载 table 视图。但是,我还想在 table 视图的末尾添加一个新单元格,以便用户可以将另一个项目添加到他们的列表中。
这就是我正在做的尝试:
dispatch_async(dispatch_get_main_queue(), {
do {
try self.fetchedResultsController.performFetch()
} catch {
fatalError("Fetch failed: \(error)")
}
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.fetchedResultsController.sections!.first!.numberOfObjects - 1, inSection: 0)], withRowAnimation: .Automatic)
self.tableView.reloadData()
})
添加了新单元格,但没有行动画。我认为我对 GCD 和 Core Data 有一些误解。感谢您的帮助。
需要使用第一个闭包作为异步处理运行的线程。该过程完成后,它将进入主线程上的第二个闭包,以对您的视图进行任何操作
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// process here
do {
try self.fetchedResultsController.performFetch()
} catch {
fatalError("Fetch failed: \(error)")
}
dispatch_async(dispatch_get_main_queue(), { Void in
// then refresh UI
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.fetchedResultsController.sections!.first!.numberOfObjects - 1, inSection: 0)], withRowAnimation: .Automatic)
self.tableView.reloadData()
})
}
查看本教程了解详情 https://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1
通过实施解决了这个问题:
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
在此函数内部使用 switch
语句可以更好地控制发生的每个更改:
switch type {
case NSFetchedResultsChangeType.Insert:
print("Insert")
case NSFetchedResultsChangeType.Delete:
print("Delete")
case NSFetchedResultsChangeType.Update:
print("Update")
case NSFetchedResultsChangeType.Move:
print("Move")
}
在此开关内,您可以使用 indexPath
删除单元格、添加单元格,随心所欲。只需使用 insertRowsAtIndexPaths
或其他相关函数。