如何在多个用户帐户之间切换

时间:2015-05-19 23:54:41

标签: ios swift core-data

我正在构建一个应用程序,其中用户可以拥有多个具有不同凭据(用户名,密码)的帐户来访问服务器上的数据。使用CoreData,我保存用户凭据并让用户在帐户之间切换。我遇到的问题是帐户更改在使用应用时不会生效。您必须重新启动应用才能看到效果。我想要完成的是类似于具有多个帐户的Twitter应用程序。当您选择帐户时,您会看到与该帐户关联的推文。下面是我用于存储用户凭据的代码。我需要做些什么才能获得与Twitter应用账户相似的用户体验?

// Credentials object where I use it to access user credentials
class Credentials{
    var name:String
    var url:String
    var username:String
    var password:String

    init(name:String, url:String, username:String, password:String){
        self.name = name
        self.url = url
        self.username = username
        self.password = password
    }
}

// Users managed object class for CoreData
class User: NSManagedObject {
    @NSManaged var username: String
    @NSManaged var password: String
    @NSManaged var name: String
    @NSManaged var url: String
    @NSManaged var active: NSNumber
}

// The API class is where I pull the active user and set the Credentials object later I connect to my server with those credentials. 
// Request methods are omitted

class API {

    var credentials:Credentials!

    init(){
        self.credentials = getActiveStore()
        self.params = ["username":"\(credentials.username)", "password":"\(credentials.password)"]
    }

    func getActiveStore() -> Credentials?{
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!
        let fetchRequest = NSFetchRequest(entityName: "User")
        fetchRequest.returnsObjectsAsFaults = false
        fetchRequest.predicate = NSPredicate(format: "active = %@", 1 as NSNumber)

        var error:NSError?

        let fetchResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as! [User]
        if fetchResults.count == 1 {
            let name = fetchResults[0].name
            let url = fetchResults[0].url
            let consumerKey = fetchResults[0].username
            let consumerSecret = fetchResults[0].password
            return Credentials(name: name, url: url, username: username, password: password)
        }
        return Credentials(name: "", url: "", username: "", password: "")
    }
}

此功能在我的UsersTableViewController中。当用户点击用户激活该特定用户时,它会被调用。

private func updateStore(user:User){
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext!

    let fetchRequest : NSFetchRequest = NSFetchRequest(entityName: "User")
    let fetchPredicate = NSPredicate(format: "name == %@", user.name)
    fetchRequest.predicate = fetchPredicate

    var error : NSError? = nil
    var fetchResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as! [User]
    if error != nil {
        println("An error occurred loading the data")
    } else {
        fetchResults[0].setValue(1, forKey: "active")

        var saveError : NSError? = nil
        if !managedContext.save(&saveError) {
            println("Could not update record")
        }else{
            credentials = Credentials(name: user.name, url: user.url, username: user.username, password: user.password)
        }
    }
    tableView.reloadData()
}

0 个答案:

没有答案