Reorder UicollectioView items using RealmSwift on

2019-04-17 03:50发布

问题:

I'm Trying reorder UICollectionViewcell Images on drag and drop using RealmSwift As database, My UI is not updating on a drag and drop and strange behaviour, some Images are duplicating , my code is Like this

RealmModel As

class StoryAlbumDM: Object {

    dynamic var id = 0
    dynamic var type = ""
    dynamic var isImage: Int = 0
    dynamic var textData = ""
    dynamic var imageData: NSData? = nil
    dynamic var rowId: Int = 0
    dynamic var position: Int = 0
    dynamic var storyId: Int = 0
    dynamic var isCoverImage: Int = 0
    dynamic var imagePath = ""

    let allStories = List<StoryAlbumDM>()
}

On drag and drop I'm doing Like this

func collectionView(collectionView: UICollectionView, atIndexPath: NSIndexPath, didMoveToIndexPath toIndexPath: NSIndexPath) {

        print("moveItemAtIndexPath")
        let fromIndexPath: Int = atIndexPath.row
        print("from", fromIndexPath)

        let toIndexPathInt: Int = toIndexPath.row
        print("To", toIndexPath)
        let fromData: StoryAlbumDM!
        fromData = realm.objects(StoryAlbumDM.self).filter("position = %d AND storyId = %d", fromIndexPath, self.storyID).first!
        let toData: StoryAlbumDM!
        toData = realm.objects(StoryAlbumDM.self).filter("position = %d AND storyId = %d", toIndexPath, self.storyID).first!
        var tempData = StoryAlbumDM()

        self.performSelectorOnMainThread(#selector(StoryViewController.updateSrtoryInRealm), withObject: self.collectionView, waitUntilDone: true)

        dispatch_async(dispatch_get_main_queue(), {
            self.collectionView.performBatchUpdates({
                self.collectionView.reloadData()
                }, completion: nil)
        })
  }

func updateSrtoryInRealm() {

        self.tempData.type = self.toData.type
        self.tempData.isImage = self.toData.isImage
        self.tempData.textData = self.toData.textData
        self.tempData.rowId = self.toData.rowId
        self.tempData.imageData = self.toData.imageData
        self.tempData.position = self.toData.position
        self.tempData.storyId = self.toData.storyId
        self.tempData.isCoverImage = self.toData.isCoverImage
        self.tempData.imagePath = self.toData.imagePath

        do {
            try! realm.write {

                self.toData.type = self.fromData.type
                self.toData.isImage = self.fromData.isImage
                self.toData.textData = self.fromData.textData
                self.toData.rowId = self.fromData.rowId
                self.toData.imageData = self.fromData.imageData
                self.toData.position = self.fromData.position
                self.toData.storyId = self.fromData.storyId
                self.toData.isCoverImage = self.fromData.isCoverImage
                self.toData.imagePath = self.fromData.imagePath

                // title.id = temp.id
                self.fromData.type = self.tempData.type
                self.fromData.isImage = self.tempData.isImage
                self.fromData.textData = self.tempData.textData
                self.fromData.rowId = self.tempData.rowId
                self.fromData.imageData = self.tempData.imageData
                self.fromData.position = self.tempData.position
                self.fromData.storyId = self.tempData.storyId
                self.fromData.isCoverImage = self.tempData.isCoverImage
                self.fromData.imagePath = self.tempData.imagePath
            }
            //}
        }
        catch {
            print("Printed error : ")
        }

Problem: Images Are duplicating, Not updating on UI , Reorder strange behaviour, please help me on this

回答1:

I answered a similar question recently, but I'll re-explain it here. :)

Easily, the best and quickest way to re-order Realm objects inside a Realm file is to make an overarching List object that holds all of the Realm objects of a given type.

For example in this case, you make another object to hold that allStories value you already created:

// Model class that manages the ordering of story album objects
class StoryAlbumDMList: Object {
   let allStories = List<StoryAlbumDM>()
}

// Model class for the actual story album objects
class StoryAlbumDM: Object {
   dynamic var id = 0
   dynamic var type = ""
   dynamic var isImage: Int = 0
   dynamic var textData = ""
   dynamic var imageData: NSData? = nil
   dynamic var rowId: Int = 0
   dynamic var position: Int = 0
   dynamic var storyId: Int = 0
   dynamic var isCoverImage: Int = 0
   dynamic var imagePath = ""
}

This way, when you want to re-order the list, all you need to do is re-order them inside this array.

Like I said in the other question, one other way you can do it (Which is not as good, but also doesn't require an extra Realm object) is to add another property named orderedIndex, which simply contains a number indicating the numerical order of these objects. When you want to re-order them, it's simply a matter of re-setting these numbers.

Let me know if you need any more clarification!