将 NSManagedObjects 与 JSON 数组的内容进行比较
Comparing NSManagedObjects to contents of a JSON array
我有一个 managedObjectContext
,我最初使用 JSON 文件的内容填充它。
前几个版本很容易更新 managedObjectContext
以获取新内容,但随着版本数量的增加,提供文件名以跨不同版本更新变得越来越困难。
我想做的是使用 currentVersionJSON
作为罗塞塔石碑来确定 managedObjectContext
中应该包含什么以及应该删除什么。我已经通过 JSON
找到了 "additions",但是我很难用另一种方式从 managedObjectContext
.
中删除旧内容
我已经根据 JSON 文件的内容找到了向 managedObjectContext
添加记录的方法,方法是创建一个基于 JSON 条目的谓词,如果它不存在,我加了。
走另一条路并不容易。基本上,我要做的是:
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)
}
}
}
}
感谢 Wain 为我指明了正确的方向。我的问题的解决方案非常简单。
我将问题分为 3 个部分:
1) 从 managedObjectContext
获取项目
2) 为我创建的当前版本的 JSON 文件提取 itemDescriptions
项并将 String
转储到一个 Array
用于比较。
我发现这个答案特别有用:
3) 将 managedObjectContext
中的每个项目与当前版本的 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()
}
}
我有一个 managedObjectContext
,我最初使用 JSON 文件的内容填充它。
前几个版本很容易更新 managedObjectContext
以获取新内容,但随着版本数量的增加,提供文件名以跨不同版本更新变得越来越困难。
我想做的是使用 currentVersionJSON
作为罗塞塔石碑来确定 managedObjectContext
中应该包含什么以及应该删除什么。我已经通过 JSON
找到了 "additions",但是我很难用另一种方式从 managedObjectContext
.
我已经根据 JSON 文件的内容找到了向 managedObjectContext
添加记录的方法,方法是创建一个基于 JSON 条目的谓词,如果它不存在,我加了。
走另一条路并不容易。基本上,我要做的是:
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)
}
}
}
}
感谢 Wain 为我指明了正确的方向。我的问题的解决方案非常简单。
我将问题分为 3 个部分:
1) 从 managedObjectContext
2) 为我创建的当前版本的 JSON 文件提取 itemDescriptions
项并将 String
转储到一个 Array
用于比较。
我发现这个答案特别有用:
3) 将 managedObjectContext
中的每个项目与当前版本的 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()
}
}