Updating data in UITableView is not smooth

Nilanshu Jaiswal

I am using NSFetchedResultsController to display the already saved data in the tableView. At the same time, I am checking for new data by a web service. When I receive data from the network, my database gets updated with the new data. The tableView automatically gets updated. But the update in tableView is not smooth. Please offer a suitable solution for swift 4. Please watch this video which I have uploaded for the better explanation. https://www.youtube.com/watch?v=XhSEykQcP5A&feature=youtu.be

This is my NSFetchedResultsController delegate implementation

extension CoreDataTableViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    tableView.beginUpdates()
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    let set = IndexSet(integer: sectionIndex)
    switch (type) {
    case .insert:
        tableView.insertSections(set, with: .fade)
    case .delete:
        tableView.deleteSections(set, with: .fade)
    default:
        break
    }
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch(type) {
    case .insert:
        tableView.insertRows(at: [newIndexPath!], with: .fade)
    case .delete:
        tableView.deleteRows(at: [indexPath!], with: .fade)
    case .update:
        tableView.reloadRows(at: [indexPath!], with: .fade)
    case .move:
        tableView.deleteRows(at: [indexPath!], with: .fade)
        tableView.insertRows(at: [newIndexPath!], with: .fade)
    }
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    tableView.endUpdates()
}
Nilanshu Jaiswal

I solved the issue.

Earlier, I was using saveContext only once (after the for loop), after all the NSManagedObject were generated. Now, I am using saveContext after generating each NSManagedObject. Below is the snippet of the code where I am creating the NSManagedObject and saving them.

                        for z in 0...(json.count - 1) {
                        let a = json[z]["PackageName"].stringValue
                        let b = json[z]["ExamGroupId"].stringValue
                        let c = json[z]["TestList"].stringValue
                        let d = json[z]["TestPackageId"].stringValue
                        _ = TestPackage(packageNameCD: a, examGroupIdCD: b, testListCD: c, testPackageIdCD: d, context: self.fetchedResultsControler.managedObjectContext)
                        (UIApplication.shared.delegate as! AppDelegate).saveContext() // now I am saving here itself and not after the loop.

                    }
                    // (UIApplication.shared.delegate as! AppDelegate).saveContext() // earlier, I was saving here, after the for loop.

I have yet not fully understood it. But it does solve the problem. I will be very thankful to anyone who can explain it further.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift updating UITableView with new data

From Dev

Updating UITableView when retrieving json data

From Dev

Updating data in UITableView after asynchronous load

From Dev

UITableView not updating

From Dev

UITableView scrolling is not smooth

From Dev

UITableView smooth scrolling

From Dev

UILabel in custom UITableView cell not updating with Core Data change

From Dev

UITableView with autolayout not smooth when scrolling

From Dev

Updating uitableview datasource and updating tabbar

From Dev

UITableView cells not updating in Swift

From Dev

searchDisplayController not updating UITableVIew

From Dev

UITableView not updating on reloadData

From Dev

Smooth UITableView Cell Expansion With Accordion Style

From Dev

Smooth circular data

From Dev

Smooth Data and Find Maximum

From Dev

Efficient method to smooth data in R

From Dev

Smooth Streaming Codec Private Data

From Dev

Efficient method to smooth data in R

From Dev

Smooth scroll to data-href

From Dev

objective c UITableView not scrolling smooth with custom cell & views

From Dev

UITableView Not Smooth Scrolling during dynamic cell contents change based on the settings

From Dev

Scrolling UITableView with few different types of cells not smooth enough

From Dev

UITableView contentInset not updating when keyboard interactively dismissed

From Dev

How can I prevent updating cell in UITableView?

From Dev

Updating a UITableView in Swift from another ViewController

From Dev

Updating a UITableView in Swift from another ViewController

From Dev

Updating UITableView in another UIViewController without a segue in Swift

From Dev

UITableView cellForRowAtIndexPath function not updating after reload function

From Dev

R smooth.spline(): smoothing spline is not smooth but overfitting my data

Related Related

  1. 1

    Swift updating UITableView with new data

  2. 2

    Updating UITableView when retrieving json data

  3. 3

    Updating data in UITableView after asynchronous load

  4. 4

    UITableView not updating

  5. 5

    UITableView scrolling is not smooth

  6. 6

    UITableView smooth scrolling

  7. 7

    UILabel in custom UITableView cell not updating with Core Data change

  8. 8

    UITableView with autolayout not smooth when scrolling

  9. 9

    Updating uitableview datasource and updating tabbar

  10. 10

    UITableView cells not updating in Swift

  11. 11

    searchDisplayController not updating UITableVIew

  12. 12

    UITableView not updating on reloadData

  13. 13

    Smooth UITableView Cell Expansion With Accordion Style

  14. 14

    Smooth circular data

  15. 15

    Smooth Data and Find Maximum

  16. 16

    Efficient method to smooth data in R

  17. 17

    Smooth Streaming Codec Private Data

  18. 18

    Efficient method to smooth data in R

  19. 19

    Smooth scroll to data-href

  20. 20

    objective c UITableView not scrolling smooth with custom cell & views

  21. 21

    UITableView Not Smooth Scrolling during dynamic cell contents change based on the settings

  22. 22

    Scrolling UITableView with few different types of cells not smooth enough

  23. 23

    UITableView contentInset not updating when keyboard interactively dismissed

  24. 24

    How can I prevent updating cell in UITableView?

  25. 25

    Updating a UITableView in Swift from another ViewController

  26. 26

    Updating a UITableView in Swift from another ViewController

  27. 27

    Updating UITableView in another UIViewController without a segue in Swift

  28. 28

    UITableView cellForRowAtIndexPath function not updating after reload function

  29. 29

    R smooth.spline(): smoothing spline is not smooth but overfitting my data

HotTag

Archive