我有以下代碼。如何獲取NSBatchDeleteRequest以刪除並允許您再次添加回對象
let workorderFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Workorders")
let deleteWorkOrderRequest = NSBatchDeleteRequest(fetchRequest: workorderFetchRequest) //Deletes ALL workorders
//Question is will it delete all references to the work orders correctly now?
try context.execute(deleteWorkOrderRequest)
//Save the work order objects (overwriting any old ones with same id if needed)
do {
try context.save()
print("Saved context with workorders")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
print("Could not save, unknown error")
}
print("deleted references")
一個工作訂單看起來像這樣...
import Foundation
import CoreData
extension Workorders {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Workorders> {
return NSFetchRequest<Workorders>(entityName: "Workorders");
}
@NSManaged public var address: String?
@NSManaged public var client_id: Int64
@NSManaged public var desc: String?
@NSManaged public var id: Int64
@NSManaged public var phone: String?
@NSManaged public var signaturepath: String?
@NSManaged public var lat: String?
@NSManaged public var lng: String?
@NSManaged public var service: Service?
@NSManaged public var pictures: NSSet?
@NSManaged public var videos: NSSet?
}
// MARK: Generated accessors for pictures
extension Workorders {
@objc(addPicturesObject:)
@NSManaged public func addToPictures(_ value: Pictures)
@objc(removePicturesObject:)
@NSManaged public func removeFromPictures(_ value: Pictures)
@objc(addPictures:)
@NSManaged public func addToPictures(_ values: NSSet)
@objc(removePictures:)
@NSManaged public func removeFromPictures(_ values: NSSet)
}
// MARK: Generated accessors for videos
extension Workorders {
@objc(addVideosObject:)
@NSManaged public func addToVideos(_ value: Videos)
@objc(removeVideosObject:)
@NSManaged public func removeFromVideos(_ value: Videos)
@objc(addVideos:)
@NSManaged public func addToVideos(_ values: NSSet)
@objc(removeVideos:)
@NSManaged public func removeFromVideos(_ values: NSSet)
}
我把它引用看起來像這樣的一個服務對象......
import Foundation
import CoreData
extension Service {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Service> {
return NSFetchRequest<Service>(entityName: "Service");
}
@NSManaged public var id: Int64
@NSManaged public var name: String?
@NSManaged public var templatedata: String?
@NSManaged public var workorder: Workorders?
}
當我運行刪除代碼我在工作模式上設置爲Nullify作爲刪除規則 ...(參見下圖)
當我運行批量刪除,但它似乎並沒有正確無效的服務。當我嘗試設置一個新的服務到它...崩潰的工作單
的libC++ abi.dylib:與類型的未捕獲的異常終止NSException
我敢肯定,這是這種情況,因爲如果我將刪除規則設置爲在工作單上級聯,那麼它將修復問題。但是,我想保留舊的服務只是取消參考。
爲什麼我不能在沒有崩潰的情況下將新服務設置到工作單上?
下面附加的是它通過查看控制檯消息而崩潰的代碼區域,我將其縮小到我嘗試設置服務的那一刻。
另外請注意,我使用的是NSMergeByPropertyObjectTrumpMergePolicy,因爲我打算再設定相同的ID服務工作單,所以當我創建一個新的服務具有相同ID它應該只是覆蓋如果我的理解是正確的,舊服務的數據。這可能是問題,如果是的話,我該如何解決它?