Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

realmcollection notification cause crashed to application #4656

Closed
Raghvendra7 opened this issue Feb 11, 2017 · 6 comments
Closed

realmcollection notification cause crashed to application #4656

Raghvendra7 opened this issue Feb 11, 2017 · 6 comments
Labels

Comments

@Raghvendra7
Copy link

Raghvendra7 commented Feb 11, 2017

with reference to issue #4425

Hi i m facing same issue using RealmSwift (2.3.0):
Xcode 8.2.1

I have used realmcollection notification .But got crashed while setting a filter query

tasks = tasks.filter("selfAssignment = %@","true")

Use case is suppose a filter is set on a list and new data is added in realmcollection. A Realm notification is generated which updated the table.But it crashed on tableview endupdate.

notification code are as follows

func notificationSubscription(tasks: Results) -> NotificationToken {
    return tasks.addNotificationBlock {[weak self] (changes: RealmCollectionChange<Results>) in
        self?.updateUI(changes: changes)
    }
}

func updateUI(changes: RealmCollectionChange<Results<RMCAssignmentObject>>) {
    switch changes {
    case .initial(_):
        assignmentTableView.reloadData()
    case .update(_, let deletions, let insertions, _):
        
        assignmentTableView.beginUpdates()
        
        assignmentTableView.insertRows(at: insertions.map {IndexPath(row: $0, section: 0)},
                                       with: .automatic)
        assignmentTableView.deleteRows(at: deletions.map {IndexPath(row: $0, section: 0)},
                                       with: .automatic)
        
        **# assignmentTableView.endUpdates()** . -- **crashed here**
        break
    case .error(let error):
        print(error)
    }
}

Pls provide any solution

@bdash
Copy link
Contributor

bdash commented Feb 14, 2017

You say that your application is crashing: what is the nature of the crash? Is an exception being thrown, a segfault occurring, or something else? Can you share the crash log and backtrace of the crash?

@jpsim
Copy link
Contributor

jpsim commented Feb 15, 2017

Given that this is crashing in UITableView.endUpdates(), it's highly likely that the table view's state does not reflect Realm's. @Raghvendra7 is it possible that you're inserting/moving/deleting rows from the tableview elsewhere in your code?

Since collection notifications carry "diff" information, it expects the state of the table view to match up exactly with the state of the collection being observed.

@Raghvendra7
Copy link
Author

Raghvendra7 commented Feb 15, 2017

@bdash Sure I will share the crash log shortly

@jpsim I m not updating the data from elsewhere in code but the Actually I am using this function to fetch the data from realm and filter and sort it on basis of criteria

func getTasks(sortParameter:String = "",ascendingFlag:Bool = true){
    let realm = try! Realm()
    tasks = realm.objects(RMCAssignmentObject.self)
    tasks = tasks.filter("status = %@",currentStatus.rawValue)
    // sortData()
    filterData()
    assignmentTableView.reloadData()
}

func filterData(){
    if Singleton.sharedInstance.assignedByValue != nil {
        if Singleton.sharedInstance.assignedByValue?.uppercased() == "SELF" {
            tasks = tasks.filter("selfAssignment = %@","true")
        }else{
            tasks = tasks.filter("selfAssignment = %@","false")
        }
    }
}

This function is called in viewDidAppear.

Normally the code is working fine, but it fails when I have set filter and from API end, new data is added in realm collection, it crashed.

Also the code is working fine when i comment this:

func updateUI(changes: RealmCollectionChange<Results<RMCAssignmentObject>>) {
    guard let tableView = self.assignmentTableView else { return }

    /**
    switch changes {
    case .initial(_):
        tableView.reloadData()
    case .update(_, let deletions, let insertions, let modifications):
        tableView.beginUpdates()
        tableView.insertRows(at: insertions.map {IndexPath(row: $0, section: 0)}, with: .automatic)
        tableView.deleteRows(at: deletions.map {IndexPath(row: $0, section: 0)}, with: .automatic)
        tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }), with: .automatic)
        tableView.endUpdates()
    case .error(let error):
        print(error)
    }

   
    **/   

 tableView.reloadData()
}

@bdash bdash removed their assignment Feb 20, 2017
@austinzheng
Copy link
Contributor

Sorry for the delay. Would you be willing to send over a sample project that we can run to reproduce this bug? Without understanding your code it's difficult to make any progress. You can send a zip file to [email protected]; if you do we'd be happy to continue looking into the problem.

@jpsim
Copy link
Contributor

jpsim commented Mar 9, 2017

The fact that you're changing which query is backing the table view without changing which query you're observing for change notifications is almost certainly the culprit here, @Raghvendra7.

One way to fix this would be to tear down the previous notification block and re-register:

func getTasks(sortParameter:String = "",ascendingFlag:Bool = true){
    let realm = try! Realm()
    tasks = realm.objects(RMCAssignmentObject.self)
    tasks = tasks.filter("status = %@",currentStatus.rawValue)
    // sortData()
    filterData()
    notificationToken.stop()
    notificationToken = notificationSubscription(tasks: tasks)
}

@Raghvendra7
Copy link
Author

thanks @jpsim

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants