奇怪的错误,我有24个相同实体的对象,其中只有15个保存在上下文中,具有属性on_campus = false
的人
25/25成功构建了Place对象并将其传递回VC,但on_campus上的Places不会保存在上下文中。
我是Core Data的新手,所以任何建议都会受到高度赞赏
我已经用我收到的错误更新了这篇文章的结尾,这是保存Place的关系实体,Business_Period的一个问题 的ViewController:
func getPlaces(data: JSON) -> [[PTPlace]] {
var place_prototypes: [PTPlace] = []
for (key, value) in data {
do {
if var place = try? PTPlace(data: value.dictionaryObject!) {
place_prototypes.append(place)
}
}
catch {
print(error)
}
}
self.updateDatabase(with: place_prototypes)
return place_prototypes.group(by: {$0.section_name} )
}
func updateDatabase(with prototypes: [PTPlace]) {
container?.performBackgroundTask { [weak self] context in
for prototype in prototypes {
var place = try? Place.findOrCreatePlaceFromProto(matching: prototype, in: context)
}
try? context.save()
}
}
Place.swift
class func findOrCreatePlaceFromProto(matching prototype: PTPlace, in context: NSManagedObjectContext) throws -> Place {
let request: NSFetchRequest<Place> = Place.fetchRequest()
request.predicate = NSPredicate(format: "id= %@", prototype.place_id)
do {
let matches = try context.fetch(request)
return matches[0]
}
catch {
throw error
}
let place = Place(context: context)
place.name = prototype.place_name
place.id = prototype.place_id
place.on_campus = prototype.on_campus
let place_type = try Place_Type.findOrCreatePlaceType(matches: prototype.section_name, in: context)
place.addToTypes(place_type)
if place.types?.count == 0 {
print("\(place.name!) has no types")
}
place.address = prototype.formatted_address
place.website = prototype.website
place.phone_number = prototype.phone_number
if prototype.all_hours != nil {
for hours_proto in prototype.all_hours! {
let hours = Business_Period.createBusinessPeriodFromProto(matching: hours_proto, in: context)
place.addToPeriods(hours)
}
}
print("successfully created \(String(describing: place.name!)): \(String(describing: place.id!))")
return place
}
Place_Type.swift
class Place_Type: NSManagedObject {
static var id: Int16 = 0
class func findOrCreatePlaceType(matches name: String, in context: NSManagedObjectContext) throws -> Place_Type {
if name != "On Campus" {
print("place_type name to fetch: \(name)")
}
let request: NSFetchRequest<Place_Type> = Place_Type.fetchRequest()
request.predicate = NSPredicate(format: "name= %@", name)
do {
let matches = try context.fetch(request)
if matches.count > 0 {
assert(matches.count == 1, "Place_Type.findOrCreatePlaceType -- database inconsistency")
return matches[0]
}
}
catch {
throw error
}
let place_type = Place_Type(context: context)
print("Attempting to create place_type: \(name)")
id += 1
place_type.name = name
place_type.id = id
print("successfully created place_type \(String(describing:place_type.id)): \(String(describing: place_type.name))")
return place_type
}
}
更新:这是我得到的错误
"Error Domain=NSCocoaErrorDomain Code=1630
\"The operation couldn\U2019t be completed. (Cocoa error 1630.)\"
UserInfo={NSValidationErrorObject=<Mishigas.Business_Period: 0x60000009c930> (entity: Business_Period; id: 0x600000037b00
<x-coredata:///Business_Period/t85267661-0D93-4F9B-A982-1CE66D00CDD669>
data: {
end = \"2017-09-02 04:00:00 +0000\";
name = Default;
place = \"0x600000038000
<x-coredata:///Place/t85267661-0D93-4F9B-A982-1CE66D00CDD667>\";
start = \"2017-09-01 10:00:00 +0000\";
}),
NSValidationErrorValue=2017-09-02 04:00:00 +0000,
NSValidationErrorKey=end,
NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1630.)}"
Business_Period.Swift
class func createBusinessPeriodFromProto(matching prototype: PTEvent, in context: NSManagedObjectContext) -> Business_Period {
let business_period = Business_Period(context: context)
business_period.start = prototype.start as NSDate
business_period.end = prototype.end as NSDate
business_period.name = "Default"
return business_period
}
Business Period继承自名为“Event”的抽象实体,其属性为:
name: String?
start: NSDate
end: NSDate
Event.swift
class Event: NSManagedObject {
}
答案 0 :(得分:0)
解决了这个问题!
错误虽然重复了很多次,但总是来自同一个实体和属性,所以我删除并重新添加了该属性(Business_Period.end),错误就消失了。
"Error Domain=NSCocoaErrorDomain Code=1630
\"The operation couldn\U2019t be completed. (Cocoa error 1630.)\"
UserInfo={NSValidationErrorObject=<Mishigas.Business_Period: 0x60000009c930> (**entity: Business_Period**; id: 0x600000037b00
<x-coredata:///Business_Period/t85267661-0D93-4F9B-A982-1CE66D00CDD669>
data: {
end = \"2017-09-02 04:00:00 +0000\";
name = Default;
place = \"0x600000038000
<x-coredata:///Place/t85267661-0D93-4F9B-A982-1CE66D00CDD667>\";
start = \"2017-09-01 10:00:00 +0000\";
}),
NSValidationErrorValue=2017-09-02 04:00:00 +0000,
**NSValidationErrorKey=end**,
NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1630.)}"