3lvis / DATASource

Core Data's NSFetchedResultsController wrapper for UITableView and UICollectionView
Other
106 stars 27 forks source link

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

Closed ortizroberto closed 3 years ago

ortizroberto commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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