Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Updating core data object causes DATASource to delete from tableView #122

Closed
ortizroberto opened this issue Oct 5, 2020 · 5 comments
Closed

Comments

@ortizroberto
Copy link

I am setting up a UITableView with DATASource like this:

lazy var dataSource: DATASource = {
        let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserGroup")
        request.sortDescriptors = [
            NSSortDescriptor(key: "user.isSelf", ascending: false)
        ]
        
        request.predicate = NSPredicate(format: "group.groupId == %@", group ?? "0")

        let dataSource = DATASource(tableView: self.tableView, cellIdentifier: GroupDetailMiddleTableViewCell.identifier, fetchRequest: request, mainContext: DataBaseManager.sharedManager.dataStack.mainContext, configuration: { cell, item, indexPath in
            let userGroup = item as! UserGroup
            let cellUser = cell as! GroupDetailMiddleTableViewCell
            cellUser.userGroup = userGroup
        })
        dataSource.delegate = self
        return dataSource
    }()

But after I update an object with the code below it gets deleted, even func dataSource(_ dataSource: DATASource, didDeleteObject object: NSManagedObject, atIndexPath indexPath: IndexPath) gets called

       self.dataStack.performInNewBackgroundContext { (backgroundContext) in
            let requestUserGroup = NSFetchRequest<UserGroup>(entityName: "UserGroup")
            requestUserGroup.predicate = NSPredicate(format: "group.groupId == %@ AND user.userId == %@",group,user)
            do {
                let result = try backgroundContext.fetch(requestUserGroup)
                if result.indices.contains(0) {
                    let userGroup:UserGroup = result[0]
                    userGroup.isPaused = NSNumber(value: pause)
                    try backgroundContext.save()
                }
            }
            catch {
                print("Failed to pauseUser")
            }
        }

I have been hesitant to submit an issue but I can't see where the problem is.

@3lvis
Copy link
Owner

3lvis commented Oct 6, 2020

Hola @ortizroberto,

I can't understand why that would be happening, is there any chance you could try to isolate this problem into any of the demos included in this project?

If this an offline app? Or do you do some syncing?

@ortizroberto
Copy link
Author

ortizroberto commented Oct 6, 2020

Hi @3lvis,

I'll try to isolate the problem like you're suggesting, I already saw by using Core Data Editor (an app that lets you explore SQLite) that in fact the object is being updated and not deleted.

The app does some syncing, but in this part specifically I make a request and depending on the response I do the update.

@3lvis
Copy link
Owner

3lvis commented Oct 6, 2020

@ortizroberto I imagined that the issue might be the update, probably the groupId gets updated for some reason and the item disappears from the list.

@ortizroberto
Copy link
Author

ortizroberto commented Oct 7, 2020

After days I finally found the problem. For some reason the argument in the predicate must be provided in the same type as you have it on your Model.
I changed

request.predicate = NSPredicate(format: "group.groupId == %@", group)

with

 request.predicate = NSPredicate(format: "group.groupId == %@", NSNumber(value:(group as NSString).floatValue))

and now everything is working as it should 😁.

thanks for the support @3lvis .

@3lvis
Copy link
Owner

3lvis commented Oct 7, 2020

I'm glad you got it working. Take care!

@3lvis 3lvis closed this as completed Oct 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants