领域 - 删除后无法使用对象

时间:2017-03-28 23:47:11

标签: ios objective-c swift realm realm-list

我的应用中有一个视频播放器。集合视图中有一个视频列表。如果您点击其中一个单元格,则会出现一个新的视图控制器来播放所选的视频。此外,您可以在此新视图控制器中循环浏览集合视图中的所有视频,因为传递了整个列表。

问题是: 当用户在PlayerVC时,他们可以不受Video的影响。如果他们这样做,我从Realm中删除Video对象。但是,这会导致:

Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'

基本上,如果用户正在PlayerVC观看视频并且他不喜欢视频,我希望他们仍然可以暂时观看视频。 但是当他们离开PlayerVC时,FavoritesVC中的集合视图应该更新,而不再显示Video

当我删除Video对象时,我使用了Realm的delete方法。

这是我保存Video个对象列表的代码:

/// Model class that manages the ordering of `Video` objects.
final class FavoriteList: Object {
    // MARK: - Properties

    /// `objectId` is set to a static value so that only
    /// one `FavoriteList` object could be saved into Realm.
    dynamic var objectId = 0

    let videos = List<Video>()

    // MARK: - Realm Meta Information

    override class func primaryKey() -> String? {
        return "objectId"
    }
}

这是我的Video类,它具有isFavorite属性:

final class Video: Object {
    // MARK: - Properties

    dynamic var title = ""
    dynamic var descriptionText = ""
    dynamic var date = ""
    dynamic var videoId = ""
    dynamic var category = ""
    dynamic var duration = 0
    dynamic var fullURL = ""
    dynamic var creatorSite = ""
    dynamic var creatorName = ""
    dynamic var creatorURL = ""

    // MARK: FileManager Properties (Files are stored on disk for `Video` object).

    /*
        These are file names (e.g., myFile.mp4, myFile.jpg)
    */
    dynamic var previewLocalFileName: String?
    dynamic var stitchedImageLocalFileName: String?
    dynamic var placeholderLocalFileName: String?

    /*
        These are partial paths (e.g., bundleID/Feed/myFile.mp4,     bundleID/Favorites/myFile.mp4)
        They are used to build the full path/URL at runtime.
    */
    dynamic var previewLocalFilePath: String?
    dynamic var stitchedImageLocalFilePath: String?
    dynamic var placeholderLocalFilePath: String?

    // Other code...
}

这是我在集合视图中显示Video个对象的代码(注意:我使用RealmCollectionChange更新集合视图以删除和插入单元格):

/// This view controller has a `collectioView` to show the favorites.
class FavoriteCollectionViewController: UIViewController {
    // MARK: Properties

    let favoriteList: FavoriteList = {
        let realm = try! Realm()
        return realm.objects(FavoriteList.self).first!
    }()

    // Realm notification token to update collection view.
    var notificationToken: NotificationToken?

    // MARK: Collection View

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return favoriteList.videos.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FavoritesCollectionViewCell.reuseIdentifier, for: indexPath) as! FavoritesCollectionViewCell
        cell.video = favoriteList.videos[indexPath.item]

        return cell
    }

    // I pass this lst forward to the PlayerVC
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let playerVC = self.storyboard?.instantiateViewController(withIdentifier: "PlayerViewController") as? PlayerViewController {
            // I pass the videos here.
            playerVC.videos = favoriteList.videos
            self.parent?.present(playerVC, animated: true, completion: nil)
        }
    }

    // MARK: Realm Notifications


    func updateUI(with changes: RealmCollectionChange<List<Video>>) {
        // This is code to update the collection view.
    }
}

最后,这是允许用户在所有Video个对象之间播放和循环的代码:

/// This view controller uses `AVFoundation` to play the videos from `FavoriteCollectionViewController`.
class PlayerViewControllerr: UIViewController {

    /// This `videos` is passed from `FavoriteCollectionViewController`
    var videos = List<Video>()

    // HELP: The app crashes here if I unfavorite a `Video`.
    @IBAction func didToggleStarButton(_ sender: UIButton) {
        let realm = try! Realm()
        try! realm.write {
            let videoToDelete = videos[currentIndexInVideosList] /// Get the video that is currently playing
            realm.delete(videoToDelete)
        }
    }
}

最终,我希望 完全从Realm 中删除<{1}}对象。在这种情况下,只是不确定如何/何时这样做。

有什么想法吗?

更新1

解决此问题的选项是:

  • 制作Video副本的非托管副本,并使用该副本为视图控制器的UI提供支持。

我认为这可能有效的方式是:

  • Video将收到两个PlayerVC,原始版本保存在Realm中,另一个版本List为UI提供支持。我们来调用列表ListfavoriteList

  • 所以在copyList内,我们会这样做:

代码:

didToggleStarButton

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

由于许多架构原因,这个绝对是棘手的。

你是正确的,你可以简单地从FavoriteList.videos删除对象,然后在关闭控制器时从Realm中正确删除它,但是你&#39;如果用户点击主页按钮,或者应用程序在此之前崩溃,那么你最终会得到一个无头视频对象。您需要确保能够跟踪它。

您可以考虑一些事情。

  • isDeleted属性添加到Video类。当用户不喜欢该视频时,请从Video移除FavoriteList.videos对象,将该属性设置为true,但将其保留在Realm中。稍后(当应用程序退出或视图控制器被解除时),您可以对isDeletedtrue的所有对象执行常规查询,然后删除它们(这解决了无头问题)。
  • 由于您的体系结构要求视图控制器依赖于可以从其下删除的模型,因此根据您从Video对象中使用的信息量,可能更安全地使其不受管理Video副本的副本,并使用该副本为视图控制器的UI供电。您可以通过执行let unmanagedVideo = Video(value: video)
  • 来创建现有Realm对象的新副本

答案 1 :(得分:0)

以下是解决方法。检查它是否有效。

 class PlayerViewControllerr: UIViewController {

          var arrayForIndex = [Int]()
          var videos = List<Video>()


          @IBAction func didToggleStarButton(_ sender: UIButton) {
               self.arrayForIndex.append(currentIndexInVideosList)     
           }

         @overide func viewWillDisappear(_ animated : Bool){
              super.viewWillDisappear(animated)
              for i in arrayForIndex{
                 let realm = try! Realm()
        try! realm.write {
            let videoToDelete = videos[i] /// Get the video that is currently playing
            realm.delete(videoToDelete)
        }
       }
        }