这是这个问题的后续行动,因为我可能做错了什么。
我的应用程序中有一个视频播放器。在名为 的集合视图中有一个视频列表FavoritesVC
。如果您点击其中一个单元格,则会出现一个名为的新视图控制器PlayerVC
来播放选定的视频。此外,您可以在这个新视图控制器中循环浏览集合视图中的所有视频,PlayerVC
因为整个列表都已传递。
这里一切正常。
问题是当我想收藏/取消收藏视频时。根据我上面发布的问题,我决定从答案中使用它:
将 isDeleted 属性添加到 Video 类。当用户取消收藏视频时,从 FavoriteList.videos 中删除 Video 对象,将该属性设置为 true,但将其保留在 Realm 中。稍后(当应用程序退出或视图控制器被关闭时),您可以对所有 isDeleted 为 true 的对象进行一般查询,然后删除它们(这解决了无头问题)。
只有在FavoritesVC
点击单元格时,我才能让它工作,我将领域转换List
为 Swiftarray
并使用它array
来为PlayerVC
. 如果我不这样做,我从 中删除一个对象,List
然后我尝试在 中循环List
,我得到一个Index out of range error...
.
我必须将其转换List
为 Swiftarray
并将其传递给PlayerVC
工作但在使用 Realm 时似乎错误的解决方案。最佳做法是永远不要进行这种转换,但在这种情况下,我不知道如何简单地使用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"
}
}
我将 List 转换为数组并像这样创建 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
}
}
}
有什么想法吗?