由于我可能做错了事,这是对此question的后续处理。Realm - 从列表中删除和添加对象
我在我的应用程序中有一个视频播放器。在收集视图中有一个名为FavoritesVC
的视频列表。如果点击其中一个单元格,将会出现一个名为PlayerVC
的新视图控制器来播放选定的视频。此外,您可以在新视图控制器PlayerVC
中的收藏视图中循环播放所有视频,因为整个列表都已通过。
这里的一切正常。
问题是,当我想要最喜欢/不喜欢的视频。从我上面发布的问题中,我决定从答案中使用它:
将isDeleted属性添加到Video类。当用户 不利于视频时,从 FavoriteList.videos中删除Video对象,将该属性设置为true,但将其留在Realm中。 稍后(当应用程序退出或视图控制器被解除时,视图控制器被解除),然后可以对 isDeleted为true的所有对象执行一般查询,然后将其删除(这样可以解决无头问题 问题)。
我只能得到当细胞被窃听这个工作,如果在FavoritesVC
,我的境界List
转换为雨燕array
并使用此array
到PlayerVC
供电。如果我不这样做,并且我从List
中删除了一个对象,并且我试图循环访问List
,我得到一个Index out of range error...
。
的解决方案,我必须将List
转换为雨燕array
并传递至PlayerVC
作品,但使用时境界好像不对。最佳做法是永远不会做这种转换,但在这种情况下,我不知道如何简单地使用List
这是我的名单代码:
/// 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"
}
}
我的列表转换为数组并创建PlayerVC,如下所示:
class FavoritesViewController: UIViewController {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let playerVC = self.storyboard?.instantiateViewController(withIdentifier: "playerVC") as? PlayerViewController {
// I convert the `List` to a Swift `array` here.
playerVC.videos = Array(favoritesManager.favoriteList.videos)
playerVC.currentVideoIndex = indexPath.row
self.parent?.present(playerVC, animated: true, completion: nil)
}
}
}
这是PlayerVC
的片段。这被当FavoritesVC
细胞被窃听创建:
class PlayerViewControllerr: UIViewController {
// Array passed from `FavoritesVC`. I converted this from the `List`.
var videos = [Video]()
var currentVideoIndex: Int!
var currentVideo: Video {
return videos[currentVideoIndex]
}
// HELP
// Example of how to cycle through videos. This cause crash if I use the `List` instead of Swift `array`.
func playNextVideo() {
if (currentVideoIndex + 1) >= videos.count {
currentVideoIndex = 0
} else {
currentVideoIndex = currentVideoIndex + 1
}
videoPlaybackManager.video = currentVideo
}
// This is where I add and remove videos from the `List`
@IBAction func didToggleFavoriteButton(_ sender: UIButton) {
favoritesManager.handleFavoriting(currentVideo, from: location) { [weak self] error in
if let error = error {
self?.present(UIAlertController.handleErrorWithMessage(error.localizedDescription, error: error), animated: true, completion: nil)
}
}
}
}
最后,代码从List
添加和删除的对象:
class FavoritesManager {
let favoriteList: FavoriteList
init(favoriteList: FavoriteList) {
self.favoriteList = favoriteList
}
/// Adds or removes a `Video` from the `FavoriteList`.
private func handleAddingAndRemovingVideoFromFavoriteList(_ video: Video) {
if isFavorite(video) {
removeVideoFromFavoriteList(video)
} else {
addVideoToFavoriteList(video)
}
}
/// Adds a `Video` to the `FavoriteList`.
///
/// Modifies `Video` `isDeleted` property to false so no garbage collection is needed.
private func addVideoToFavoriteList(_ video: Video) {
let realm = try! Realm()
try! realm.write {
favoriteList.videos.append(video)
video.isDeleted = false
}
}
/// Removes a `Video` from the `FavoriteList`.
///
/// Modifies `Video` `isDeleted` property to true so garbage collection is needed.
/// Does not delete the `Video` from Realm or delete it's files.
private func removeVideoFromFavoriteList(_ video: Video) {
let predicate = NSPredicate(format: "objectId = %@", video.objectId)
guard let index = favoriteList.videos.index(matching: predicate) else { return }
let realm = try! Realm()
try! realm.write {
favoriteList.videos.remove(objectAtIndex: index)
video.isDeleted = true
}
}
}
有什么想法?
感谢您的回答!这修复了崩溃!然而,经验与Swift数组不同(除非我做错了什么)。现在发生了什么:假设'PlayerVC'有2个视频。如果你不喜欢其中的一个,那么它会从'favoriteList' - >中移除,但是现在当你执行'playNextVideo'后,你不能在'PlayerVC'中循环回去,因为它不再在'List'中。 Swift'array'的用户体验是:即使视频不合适,即使它不在'PlayerVC'中的'favoriteList'中,您仍然可以循环并再次看到它。 – JEL
抱歉,如果解释有限题。但最终如果Swift'array'是一个不错的选择,那么如果这很好,我也可以使用它呢? – JEL
不用担心!是的,这是有意的。就像我上面写的那样,我假设一旦它不合适,一旦用户导航,它就会完全移除。啊,所以你真的希望视频留在视频播放器的播放列表中,直到他们解散它为止?好的。是的,制作'videos'对象的静态副本对于那个很有用。 – TiM