我在xcdatamodeld中有一个包含3个实体的快捷项目:Access,CustomerInfo和User。我试图在用户中单独保存dateEndSubscription。当我尝试保存dateEndSubscription时,它提供了空值。
我的代码有什么错误?
我的代码在这里:
import UIKit
import SwiftyJSON
import CoreData
class DataManager: NSObject {
static let sharedInstance = DataManager()
func loadUser() -> User? {
let users = loadEntity(name: Entity.User)
if let user = users.first as? User {
return user
}
return nil
}
//this for deleteuser
func deleteUser() {
deleteEntity(entityName: Entity.User)
}
func saveUser(userInfo:JSON, handler: @escaping (Bool) -> Void) {
deleteEntity(entityName: Entity.User)
deleteEntity(entityName: Entity.Access)
let entity = createEntity(withName:Entity.User)
let info = userInfo.dictionaryValue
let userId = (info[API.Id]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let dateEnd = (info[API.DateEnd]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
// this produces with no value.
let type = (info[API.UserType]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let cyberAccount = (info[API.CyberAccount]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let product = (info[API.Product]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let user = User(entity: entity, insertInto: context)
user.setValue(userId, forKey:API.Id)
user.setValue(dateEnd, forKey: API.DateEnd)
user.setValue(type, forKey:API.UserType)
user.setValue(cyberAccount, forKey:API.CyberAccount)
user.setValue(product, forKey:API.Product)
let accessInfo = info[API.Access]!
saveUserAccess(accessInfo: accessInfo)
handler(save)
}
//this for saving useraccess
func saveUserAccess(accessInfo:JSON) -> Void {
let entity = createEntity(withName:Entity.Access)
let info = accessInfo.dictionaryValue
let photo = (info[API.Photo]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let chat = (info[API.Chat]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let product = (info[API.Product]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let profile = (info[API.Profile]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let curve = (info[API.Curve]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let weeks = (info[API.Weeks]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let recipe = (info[API.Recipe]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let shopping = (info[API.Shopping]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let rdv = (info[API.RDV]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let userAccess = User(entity: entity, insertInto: context)
userAccess.setValue(photo, forKey:API.Photo)
userAccess.setValue(chat, forKey:API.Chat)
userAccess.setValue(product, forKey:API.Product)
userAccess.setValue(profile, forKey:API.Profile)
userAccess.setValue(curve, forKey:API.Curve)
userAccess.setValue(weeks, forKey:API.Weeks)
userAccess.setValue(recipe, forKey:API.Recipe)
userAccess.setValue(shopping, forKey:API.Shopping)
userAccess.setValue(rdv, forKey:API.RDV)
}
// This for saving customer info
func saveCustomerInformation(customerInfo: JSON, handler: @escaping (Bool) -> Void) {
let entity = createEntity(withName:Entity.CustomerInfo)
let info = customerInfo.dictionaryValue
let userId = (info[API.Id]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let type = (info[API.UserType]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let cyberAccount = (info[API.CyberAccount]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let product = (info[API.Product]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
let dateEnd = (info[API.DateEnd]?.stringValue ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
print("Butwal",dateEnd)
let user = User(entity: entity, insertInto: context)
user.setValue(userId, forKey:API.Id)
user.setValue(type, forKey:API.UserType)
user.setValue(cyberAccount, forKey:API.CyberAccount)
user.setValue(product, forKey:API.Product)
user.setValue(dateEnd, forKey: API.DateEnd)
print("Butwal1",dateEnd)
let accessInfo = info[API.Access]!
saveUserAccess(accessInfo: accessInfo)
handler(save)
}
//MARK: Load and Save Objects for Key
func loadObject(forKey key:String) -> Any? {
let userDefaults = UserDefaults.standard
guard let object = userDefaults.object(forKey: key) else {
return nil
}
return object as AnyObject
}
func saveObject(object:Any, forKey key:String) {
let userDefaults = UserDefaults.standard
userDefaults.set(object, forKey: key)
userDefaults.synchronize()
}
private func loadEntity(name : String) -> [Any] {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:name)
let objects = try! context.fetch(fetchRequest)
return objects
}
// MARK: Delete Entities
private func deleteEntity(entityName name:String) {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: name)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
deleteRequest.resultType = .resultTypeObjectIDs
do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
guard let objectIDs = result?.result as? [NSManagedObjectID] else { return }
let changes = [NSUpdatedObjectsKey: objectIDs]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [context])
}
catch {
fatalError("Failed to execute request: \(error)")
}
}
private func createEntity(withName name:String) -> NSEntityDescription {
return NSEntityDescription.entity(forEntityName: name, in: context)!
}
private var context: NSManagedObjectContext {
return CoreDataManager.sharedInstance.context
}
private var save: Bool {
return CoreDataManager.sharedInstance.saveContext(rollBackOnError: true)
}
}
struct API {
static let DateEnd = "dateEndSubscription"
}
Here are the screenshots for the data model of coredata. I have included all 3 entities with their relationships and inverse.
我需要来自服务器的dateEndSubscription值。但是,当我解析时,它不返回任何值。我还在用户实体中创建了dateEndSubscription属性。