【发布时间】:2017-04-12 20:43:03
【问题描述】:
这是question 的后续行动,因为我可能做错了什么。
我的应用中有一个视频播放器。在名为FavoritesVC 的集合视图中有一个视频列表。如果您点击其中一个单元格,则会出现一个名为PlayerVC 的新视图控制器来播放选定的视频。此外,您可以在这个新的视图控制器PlayerVC 中循环浏览集合视图中的所有视频,因为整个列表都已传递。
这里一切正常。
问题是当我想收藏/取消收藏视频时。根据我上面发布的问题,我决定从答案中使用它:
向 Video 类添加一个 isDeleted 属性。当用户 取消收藏视频,从 FavoriteList.videos,将该属性设置为 true,但将其保留在 Realm 中。 稍后(当应用程序退出或视图控制器 解雇),然后您可以对所有对象进行一般查询 isDeleted 为真,然后删除它们(这解决了无头 问题)。
只有在FavoritesVC 中点击单元格时,我才能将其工作,我将领域List 转换为Swift array 并使用此array 为PlayerVC 供电。如果我不这样做,我从List 中删除一个对象并尝试循环通过List,我会得到一个Index out of range error...。
我必须将 List 转换为 Swift array 并将其传递给 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 的sn-p。当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
}
}
}
有什么想法吗?
【问题讨论】:
标签: ios swift realm realm-list