插入新实体时出现错误异常
bad exception when inserting new entity
当我在核心数据中插入新实体时,出现错误访问代码 2 异常。
class func createInManagedObjectContext(moc: NSManagedObjectContext, fuel_id: Int, fuel_name: String, price: Double) -> Fuel
{
var fuel = NSEntityDescription.insertNewObjectForEntityForName("GasStation", inManagedObjectContext: moc) as Fuel //exception - EXC_ARM_BREAKPOINT
moc.performBlockAndWait { () -> Void in
fuel.fuel_id = fuel_id
fuel.fuel_name = fuel_name
fuel.price = price
}
// dispatch_async(dispatch_get_main_queue(), { () -> Void in
// fuel.fuel_id = fuel_id
// fuel.fuel_name = fuel_name
// fuel.price = price
//
// })
return fuel
}
和
if let prices = info["prices"] as? NSArray {
for price in prices {
let fuelId : Int = price["fuel_id"] as Int
var fuelPriceString : String = price["price"] as String
let fuelName : String = price["fuel_name"] as String
var fuelPrice : Double = NSString(string:fuelPriceString).doubleValue
println(managedObjectContext)
var fuel =
Fuel.createInManagedObjectContext(managedObjectContext!,fuel_id: Int64(fuelId), fuel_name: fuelName, price:fuelPrice)// exception
fuelSet.append(fuel)
}
println(fuelSet)
}
我试图查看上下文是否为零,但它有地址。我读了一些关于僵尸工具的文章,用于检测你有什么确切的异常以及为什么,但没有说明。有人可以帮助我吗,因为我真的卡住了?
您需要确保以下其中一项:
- 你的 managedObjectContext 是一个 NSMainQueueConcurrencyType(而不是
比 NSPrivateQueueConcurrencyType )
- 在 performBlock 或 performBlockAndWait 中执行更改
所以:
var fuel
moc.performBlockAndWait { () -> Void in
fuel = NSEntityDescription.insertNewObjectForEntityForName("GasStation", inManagedObjectContext: moc) as Fuel
fuel.fuel_id = fuel_id
fuel.fuel_name = fuel_name
fuel.price = price
}
return fuel
(希望容易翻译成 Swift)基本上,如果您在私有队列上下文中,请确保您不从主线程访问它(例如 UI 代码等),这适用于读取和写入。如果您是在私有队列中创建的,请小心从主线程中读取新创建的对象。
我建议花点时间阅读它并尝试看看痛点在哪里。它将为您节省漫长的时间 运行!
http://code.tutsplus.com/tutorials/core-data-from-scratch-concurrency--cms-22131
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdConcurrency.html
当我在核心数据中插入新实体时,出现错误访问代码 2 异常。
class func createInManagedObjectContext(moc: NSManagedObjectContext, fuel_id: Int, fuel_name: String, price: Double) -> Fuel
{
var fuel = NSEntityDescription.insertNewObjectForEntityForName("GasStation", inManagedObjectContext: moc) as Fuel //exception - EXC_ARM_BREAKPOINT
moc.performBlockAndWait { () -> Void in
fuel.fuel_id = fuel_id
fuel.fuel_name = fuel_name
fuel.price = price
}
// dispatch_async(dispatch_get_main_queue(), { () -> Void in
// fuel.fuel_id = fuel_id
// fuel.fuel_name = fuel_name
// fuel.price = price
//
// })
return fuel
}
和
if let prices = info["prices"] as? NSArray {
for price in prices {
let fuelId : Int = price["fuel_id"] as Int
var fuelPriceString : String = price["price"] as String
let fuelName : String = price["fuel_name"] as String
var fuelPrice : Double = NSString(string:fuelPriceString).doubleValue
println(managedObjectContext)
var fuel =
Fuel.createInManagedObjectContext(managedObjectContext!,fuel_id: Int64(fuelId), fuel_name: fuelName, price:fuelPrice)// exception
fuelSet.append(fuel)
}
println(fuelSet)
}
我试图查看上下文是否为零,但它有地址。我读了一些关于僵尸工具的文章,用于检测你有什么确切的异常以及为什么,但没有说明。有人可以帮助我吗,因为我真的卡住了?
您需要确保以下其中一项:
- 你的 managedObjectContext 是一个 NSMainQueueConcurrencyType(而不是 比 NSPrivateQueueConcurrencyType )
- 在 performBlock 或 performBlockAndWait 中执行更改
所以:
var fuel
moc.performBlockAndWait { () -> Void in
fuel = NSEntityDescription.insertNewObjectForEntityForName("GasStation", inManagedObjectContext: moc) as Fuel
fuel.fuel_id = fuel_id
fuel.fuel_name = fuel_name
fuel.price = price
}
return fuel
(希望容易翻译成 Swift)基本上,如果您在私有队列上下文中,请确保您不从主线程访问它(例如 UI 代码等),这适用于读取和写入。如果您是在私有队列中创建的,请小心从主线程中读取新创建的对象。
我建议花点时间阅读它并尝试看看痛点在哪里。它将为您节省漫长的时间 运行!
http://code.tutsplus.com/tutorials/core-data-from-scratch-concurrency--cms-22131 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdConcurrency.html