无法快速从核心数据存储中保存/检索数据

时间:2018-09-07 21:53:49

标签: ios swift core-data

我正在尝试制作一个简单的应用程序来存储和检索用户数据。

具有一个RegisterViewController.swift,其中包含用户输入要注册的字段(名称,loginUsername,loginPassword,照片)。

我有一个User.swift类将这些信息保存在一个对象中

使用CoreData,我设置了具有四个字段作为属性的“用户”实体。

我已经引用了以下链接来保存和检索数据到Users表。
https://medium.com/xcblog/core-data-with-swift-4-for-beginners-1fc067cca707https://www.bobthedeveloper.io/blog/beginner-guide-to-core-data-in-swift

但是,调试时看不到任何保存或检索的数据。我也没有看到任何例外。

以下是代码段

let name = self.name.text;
let loginUsername = self.loginUsername.text;
let loginPassword = self.loginPassword.text;
let photo = self.photo.image;

print("Setting user data in the User object");
user = User(name: name!, loginUsername: loginUsername!, loginPassword: loginPassword!, photo: photo)!;

print("store data in DB")
//Setup delegate and context 
let appDelegate = UIApplication.shared.delegate as! AppDelegate;
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: context);
let newUser = NSManagedObject(entity: entity!, insertInto: context)

//Set values for the attributes        
newUser.setValue(name, forKey: "name")
newUser.setValue(loginUsername, forKey: "loginUsername")
newUser.setValue(loginPassword, forKey: "loginPassword")
//TODO: store the photo

//Save data         
do {
    print("Trying to same")
    try context.save()
 } catch {
    print("Failed to save user")
    let nserror = error as NSError
    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
 }

 //Method 1: to get Users after saving       
 do {
     usersSaved = try context.fetch(Users.fetchRequest())
 } catch {
     print("Failed to fetch users")
     let nserror = error as NSError
     fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
 }

 //Method 2: to get Users after saving
 var usersSaved: [Users] = []
 let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users");
 request.returnsObjectsAsFaults = false
 do {
    let result = try context.fetch(request)
     print("Trying to fetch")
     for data in result as! [NSManagedObject] {
         print(data.value(forKey: "name") as! String)
     }
  } catch {
    print("Failed")
  }

这是我的User.swift类

class User {

    //MARK: Properties
    var name: String;
    var loginUsername: String;
    var loginPassword: String;
    var photo: UIImage? = nil;

    //MARK: Initialization
    init?(name: String, loginUsername: String, loginPassword: String, photo: UIImage?) {

        //Initialization should fail if there is no name or username or password
        if (name.isEmpty || loginUsername.isEmpty || loginPassword.isEmpty) {
            return nil;
        }

        //Initialize stored properties
        self.name = name;
        self.loginUsername = loginUsername;
        self.loginPassword = loginPassword;
        self.photo = photo;
     }
}

我一直在网上寻找答案,但无法找出答案。任何指示或建议都会真正帮助我进步。

1 个答案:

答案 0 :(得分:0)

对于那些可能会遇到此问题的人,我正在回答自己的问题。谢谢@ Paulw11的建议。 Xcode的行为很奇怪,我不得不执行Project->多次清理,并在CoreData实体得到解析之前删除DerivedData文件夹。

这就是我所做的

const IndexAuth = () => {
  navigate(routes.DASHBOARD);
  return null;
};