我有一个 managedObjectContext
,最初用 JSON 文件的内容填充它。
前几个版本很容易更新managedObjectContext
以获取新内容,但随着版本数量的增加,提供内容的文件名以跨不同版本进行更新变得越来越困难。
我想做的是使用currentVersionJSON
作为罗塞塔石碑来确定managedObjectContext
中应该包含什么以及应该删除什么。我已经通过 JSON
搞清楚了“添加”,但我很难采用其他方式从 managedObjectContext
中删除旧内容。
我已经通过基于 JSON 条目创建谓词来计算出如何根据 JSON 文件的内容向 managedObjectContext
添加记录,如果它不存在,我会添加它。
走另一条路并不那么容易。基本上,我想做的是:
1) 创建一个 mocItems
数组,其中包含 managedObjectContext
2) 创建一个 jsonItems
数组,其中包含当前发布版本的内容
3) 如果 managedObjectContext
中的某个项目不在 jsonItems
中,请将其附加到要删除的对象列表中。
现在,它不会崩溃,但它只是运行、运行、运行。我无法解决这个问题。我想避免添加 CocoaPods,因为当我想继续前进时,我必须经历并重构一堆东西。任何关于我搞砸的事情的反馈将不胜感激。
do {
// Get all items in MOC
var mocItems = [Item]()
// Initialize a fetchRequest
let fetchRequest = NSFetchRequest()
// Tell what entity you want to search
let entityDescription = NSEntityDescription.entityForName("Item", inManagedObjectContext: self.managedObjectContext)
fetchRequest.entity = entityDescription
// Execute fetch and assign arrayOfItems to it
do {
mocItems = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Item]
} catch {
// failure
print("Fetch failed: \(error)")
}
// Get the list of items from the currently shipping version
// Tell where the data is located
let jsonData = NSData(contentsOfURL: itemsToAddURL!)
var jsonItems = NSArray()
// Create an array to dump data from JSON file into
do {
jsonItems = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
} catch {
print("json error: \(error)")
}
for item in jsonItems {
let itemDescription = item.valueForKey("itemDescription") as! String
for object in mocItems {
if itemDescription != object.itemDescription {
itemsToDelete.append(object)
}
}
}
}
最佳答案
感谢韦恩为我指明了正确的方向。我的问题的解决方案非常简单。
我将问题分为 3 个部分:
1) 从 managedObjectContext
获取项目
2) 提取我创建的当前版本 JSON 文件的项目 itemDescriptions
并将 String
转储到 用于比较的数组
。
我发现这个答案特别有帮助:
https://stackoverflow.com/a/31859336/4475605
3) 将 managementObjectContext
中的每个项目与当前版本的 JSON 内容进行比较。
我搞砸了第 2 步和第 3 步。它们现在是这样的:
func deleteItems() {
// Create a landing spot for data from MOC & list of items in current version
// All the items in the MOC
var mocItems = [Item]()
var itemsToDelete = [Item]()
// Name of items from the current version
var itemNamesArray = [String]()
do {
// Initialize a fetchRequest
let fetchRequest = NSFetchRequest()
// Tell what entity you want to search
let entityDescription = NSEntityDescription.entityForName("Item", inManagedObjectContext: self.managedObjectContext)
fetchRequest.entity = entityDescription
// Execute fetch and assign mocItems to it
do {
mocItems = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Item]
} catch {
// failure
print("Fetch failed: \(error)")
}
}
// Get the list of items from the currently shipping version
// Tell where the data is located
let jsonData = NSData(contentsOfURL: itemsToAddURL!)
var jsonItems = NSArray()
// Create an array to dump data from JSON file into
do {
jsonItems = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
} catch {
print("json error: \(error)")
}
for item in jsonItems as! [Dictionary<String, AnyObject>] {
let itemDescription = item["itemDescription"] as! String
itemNamesArray.append(itemDescription)
}
for item in mocItems {
if itemNamesArray.contains(item.itemDescription) {
// keep it
} else {
itemsToDelete.append(item)
}
}
for item in itemsToDelete {
managedObjectContext.deleteObject(item)
saveContext()
}
}
关于ios - 将 NSManagedObjects 与 JSON 数组的内容进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35398337/