NsUserDefualts not functioning right

穿精又带淫゛_ 提交于 2019-12-12 00:37:31

问题


Basically I have some sort of bug with tableView, i noticed that my tableView is not always getting updated correctly, and i tried debugging it and what i noticed is that tableView class is not always getting called to update the table. What am i doing wrong ? When i add new entry to my table, count 4 + 1, I go to history tab, and nothing happens and it shows as the count is still 4 but if I switch tabs 1 more time it will show count as 5 and tableView will be updated.. so there is a delay in update for some reason, i could add a refresh button but i don't want to do that..

//
//  SecondViewController.swift
//
//  Created by Artiom Sobol on 1/3/16.
//  Copyright © 2016 Artiom Sobol. All rights reserved.
//

import UIKit

class History: UIViewController, UITableViewDataSource, UITableViewDelegate
{
    // test variable
    var test: MyHistory!
    // array to store unarchived history
    var newHistory = [MyHistory]()

    //outlet for tableview

    @IBOutlet var tableView: UITableView!


    override func viewDidLoad()
    {
        //change the background
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "newBackground.jpg")!)
        super.viewDidLoad()

        //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "historyCell")
        //unarchive any new data
        let defaults = NSUserDefaults.standardUserDefaults()

        if let savedPeople = defaults.objectForKey("MyHistory") as? NSData {
            newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
        }
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
    }



    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
    {
        return self.newHistory.count
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath) as! historyCell
        let person = newHistory[indexPath.item]
        let defaults2 = NSUserDefaults.standardUserDefaults()

        print("This is count", newHistory.count)

        if let savedPeople = defaults2.objectForKey("MyHistory") as? NSData {
            newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
        }



       // cell.durationLabel.text = String(person.durationNumber)
        let (hour,minutes,seconds) = secondsToHoursMinutesSeconds(person.durationNumber)

        if(seconds < 10 && minutes < 10)
        {
            cell.durationLabel.text = "0\(hour):0\(minutes):0\(seconds)"
        }
        else if(seconds > 9 && minutes < 10)
        {
            cell.durationLabel.text = "0\(hour):0\(minutes):\(seconds)"
        }
        else if(seconds > 9 && minutes > 9)
        {
            cell.durationLabel.text = "0\(hour):\(minutes):\(seconds)"
        }
        else if(seconds < 10 && minutes > 9)
        {
            cell.durationLabel.text = "0\(hour):\(minutes):0\(seconds)"
        }


        cell.kicksLabel.text = String(person.kicksNumber)

        return cell
    }


    func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int)
    {
        return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
    }


}

回答1:


reloadData is to be called when your array is changed, if you don't change your array, or when you call reloadData but no array update, no effect.

So basically, everytime you updated the array, call reloadData on main queue (it's a must if your array updating is in another queue)

So to the code:

    if let savedPeople = defaults.objectForKey("MyHistory") as? NSData {
        newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
    }
    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()

you only call reloadData() in viewDidLoad, which is only called once, when the view controller is loaded.

You can do something like:

self.newHistory = getUpdated()

dispatch_async(dispatch_get_main_queue(), {
    self.tableView.reloadData()
})



回答2:


A couple of things:

  1. After storing your data in user defaults, call synchronize once so changes are saved to permanent storage.

  2. Don't red the defaults for each row of your table view (tableView(_: cellForRowAtIndexPath:)). Instead, read them once and store the data in an array/dictionary property, and use that to configure each cell on demand.

Regarding advice #2: I don't think reading the user's defaults is too slow, but I'm not sure how big your archived data is and that method gets called for each row that the table view needs to display on screen. Be efficient!



来源:https://stackoverflow.com/questions/34985768/nsuserdefualts-not-functioning-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!