我一直在慢慢地构建我的应用程序。以前,我在CoreData中有两个实体。一个名为Items,另一个名为Catalog。目标是让我的应用程序记住添加的项目,以便用户不必再次添加所有属性,并且Catalog将存储项目的所有属性,但Items将是项目的当前活动实例
下面是我工作的代码,其中Catalog和Items都是重复信息,如果我更新了记录,则didChangeObject和didChangeSection都被正确调用,我的tableview将正确更新。
func editItem() {
item!.name = trimSpaces(itemName.text!)
item!.brand = trimSpaces(itemBrand.text!)
item!.qty = Float(itemQty.text!)
item!.price = currencyToFloat(itemPrice.text!)
item!.size = trimSpaces(itemSize.text!)
item!.section = trimSpaces(itemSection.text!)
item!.image = UIImageJPEGRepresentation(itemImage.image!, 1)
catalog!.name = trimSpaces(itemName.text!)
catalog!.brand = trimSpaces(itemBrand.text!)
catalog!.qty = Float(itemQty.text!)
catalog!.price = currencyToFloat(itemPrice.text!)
catalog!.size = trimSpaces(itemSize.text!)
catalog!.section = trimSpaces(itemSection.text!)
catalog!.image = UIImageJPEGRepresentation(itemImage.image!, 1)
do {
try moc.save()
} catch {
fatalError("Edit Item save failed")
}
if (itemProtocal != nil) {
itemProtocal!.finishedEdittingItem(self, item: self.item!)
}
}
但是当我删除Catalog和Items之间的所有重复属性并添加一对一的关系时。现在只调用didChangeObject而不调用didChangeSection。 * 更新了pbasdf建议 *
func editItem() {
let item: Items = self.item!
item.qty = Float(itemQty.text!)
item.catalog!.name = trimSpaces(itemName.text!)
item.catalog!.brand = trimSpaces(itemBrand.text!)
item.catalog!.qty = Float(itemQty.text!)
item.catalog!.price = currencyToFloat(itemPrice.text!)
item.catalog!.size = trimSpaces(itemSize.text!)
item.catalog!.section = trimSpaces(itemSection.text!)
item.catalog!.image = UIImageJPEGRepresentation(itemImage.image!, 1)
do {
try moc.save()
} catch {
fatalError("Edit Item save failed")
}
if (itemProtocal != nil) {
itemProtocal!.finishedEdittingItem(self, item: self.item!)
}
}
**注意:在第一个代码中,我访问了在Class级别设置的项目和目录变量。但是在新版本中,我希望它能够被解包,所以我在函数内部定义了项目。目录现在来自这段关系。
下面是我的didChangeObject和didChangeSection代码,它们在新版本和旧版本之间没有变化。
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case NSFetchedResultsChangeType.Update:
self.tableView.reloadSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
case NSFetchedResultsChangeType.Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
case NSFetchedResultsChangeType.Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
case NSFetchedResultsChangeType.Move:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case NSFetchedResultsChangeType.Delete:
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
case NSFetchedResultsChangeType.Insert:
self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
case NSFetchedResultsChangeType.Update:
self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
case NSFetchedResultsChangeType.Move:
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
我在iOS编程方面还很陌生,但我对正在发生的事情的猜测是,tableview正在被Items Entity填充。之前,tableview部分正在由item.section填充(在Items内部),但现在tableview部分来自与Catalog(items.catalog> catalog.section)的关系,如果这有意义的话。那么当我更新managedObectContect时,它没有注册该部分正在更新。
有一点需要注意,当我关闭应用并再次打开它时,它会修复该部分。所以它正确加载,正确插入,只是没有正确更新。我假设我需要做一些其他电话让该部分知道它正在改变,但我不知道它会是什么,谷歌并不知道答案,或者更可能的是,我的问题是它不知道答案。
感谢您提供任何帮助,如果您需要我提供屏幕截图或其他代码以便更好地了解所发生的情况,请与我们联系。
添加FRC和fetchRequest代码
func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Items")
let sortDesc1 = NSSortDescriptor(key: "catalog.section", ascending: true)
let sortDesc2 = NSSortDescriptor(key: "isChecked", ascending: true)
let sortDesc3 = NSSortDescriptor(key: "catalog.name", ascending: true)
fetchRequest.sortDescriptors = [sortDesc1, sortDesc2, sortDesc3]
return fetchRequest
}
func getFCR() -> NSFetchedResultsController {
frc = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: moc, sectionNameKeyPath: "catalog.section" , cacheName: nil)
return frc
}
添加创建新商品代码
func createNewItem() {
//Items Class Save
var entityDesc = NSEntityDescription.entityForName("Items", inManagedObjectContext: moc)
let item = Items(entity: entityDesc!, insertIntoManagedObjectContext: moc)
let id = NSUserDefaults.standardUserDefaults().integerForKey("nextItemID")
if (itemQty.text! == "") {
item.qty = 1
} else {
item.qty = Float(itemQty.text!)
}
item.isChecked = false
//Catalog Clase Save
entityDesc = NSEntityDescription.entityForName("Catalog", inManagedObjectContext: moc)
let catalog = Catalog(entity: entityDesc!, insertIntoManagedObjectContext: moc)
catalog.id = id
catalog.name = trimSpaces(itemName.text!)
catalog.brand = trimSpaces(itemBrand.text!)
if (itemQty.text == "") {
catalog.qty = 1
} else {
catalog.qty = Float(itemQty.text!)
}
catalog.price = currencyToFloat(itemPrice.text!)
catalog.size = trimSpaces(itemSize.text!)
catalog.section = trimSpaces(itemSection.text!)
catalog.image = UIImageJPEGRepresentation(itemImage.image!, 1)
item.catalog = catalog
do {
try moc.save()
NSUserDefaults.standardUserDefaults().setObject(id + 1, forKey: "nextItemID")
NSUserDefaults.standardUserDefaults().synchronize()
} catch {
fatalError("New item save failed")
}
}