在我的应用程序中,我从Realm加载存储的数据,然后调用API以获取用户对象的最新状态。之后,我过滤哪些对象是新对象,哪些应该删除,就像这样:
// tracks is an array of objects downloaded from API and passed to the method
let result = realm.objects(RealmTrack.self)
print("Discovering new tracks")
// Discover new tracks
let new = tracks.filter({ track in
!result.contains(where: { $0.trackUid == track.trackUid })
})
print("Discovering old tracks")
// Discover old tracks in database
let old = result.filter({ track in
!tracks.contains(where: { $0.trackUid == track.trackUid })
})
然后我正在执行写入事务以达到领域,它运行顺利,没有任何错误。
print("Modifying database")
try realm.write {
//realm.create(RealmTrack.self, value: realms, update: .modified)
realm.add(realms, update: Realm.UpdatePolicy.modified)
//realm.add(realms)
realm.delete(old)
}
我的问题是每个RealmTrack可以具有其他关联的Realm对象:
dynamic var _primaryAlbum: RealmAlbum?
dynamic var _primaryArtist: RealmArtist?
RealmAlbum
和RealmArtist
都有RealmTrack
个对象的列表:
let tracks = List<RealmTrack>()
删除旧曲目后,如何获得剩余的RealmAlbum
和RealmArtist
,它们没有任何关联的RealmTrack?
我也必须删除它们
我尝试过的是在写事务块之后,检查哪些对象具有空列表,但令人惊讶的是它始终为0:
//realm.objects(RealmAlbum.self).count always returns 0 here
let invalidatedAlbums = realm.objects(RealmAlbum.self).filter {
$0.tracks.count == 0
//$0.isInvalidated == true
}
let invalidatedArtists = realm.objects(RealmArtist.self).filter {
$0.tracks.count == 0
//$0.isInvalidated == true
}
try realm.write {
realm.delete(invalidatedAlbums)
realm.delete(invalidatedArtists)
}
编辑:添加了类声明
RealmTrack
@objcMembers class RealmTrack: Object, AWTrack {
dynamic var _source: String = ""
var source: AWMediaSource {
return AWMediaSource(rawValue: _source)!
}
dynamic var _trackName: String = ""
var trackName: String {
return _trackName
}
dynamic var _trackUid: String = ""
var trackUid: String {
return _trackUid
}
dynamic var _playableUrl: String? = ""
var playableUrl: String? {
return _playableUrl
}
dynamic var _duration: TimeInterval = 0
var duration: TimeInterval {
return _duration
}
dynamic var _albumTrackNumber: Int = 0
var albumTrackNumber: Int {
return _albumTrackNumber
}
dynamic var _localizedTitle: String = ""
var localizedTitle: String {
return _localizedTitle
}
dynamic var _localizedAlbumTitle: String = ""
var localizedAlbumTitle: String {
return _localizedAlbumTitle
}
dynamic var _localizedAlbumArtist: String = ""
var localizedAlbumArtist: String {
return _localizedAlbumArtist
}
dynamic var _localizedArtist: String = ""
var localizedArtist: String {
return _localizedArtist
}
dynamic var _albumUid: String = ""
var albumUid: String {
return _albumUid
}
dynamic var _artistUid: String = ""
var artistUid: String {
return _artistUid
}
dynamic var _primaryAlbum: RealmAlbum?
var primaryAlbum: AWAlbum? {
return _primaryAlbum
}
dynamic var _primaryArtist: RealmArtist?
var primaryArtist: AWArtist? {
return _primaryArtist
}
override class func primaryKey() -> String? {
return "_trackUid"
}
RealmAlbum
@objcMembers class RealmAlbum: Object, AWAlbum {
dynamic var _source: String = ""
var source: AWMediaSource {
return AWMediaSource(rawValue: _source)!
}
dynamic var _albumName: String = ""
var albumName: String {
return _albumName
}
dynamic var _albumUid: String = ""
var albumUid: String {
return _albumUid
}
dynamic var _artistName: String = ""
var artistName: String {
return _artistName
}
let tracks = List<RealmTrack>()
dynamic var _primaryArtist: RealmArtist?
var primaryArtist: AWArtist? {
return _primaryArtist
}
override class func primaryKey() -> String? {
return "_albumUid"
}
RealmArtist
@objcMembers class RealmArtist: Object, AWArtist {
dynamic var _source: String = ""
var source: AWMediaSource {
return AWMediaSource(rawValue: _source)!
}
dynamic var _artistUid: String = ""
var artistUid: String {
return _artistUid
}
dynamic var _artistName: String = ""
var artistName: String {
return _artistName
}
let tracks = List<RealmTrack>()
override class func primaryKey() -> String? {
return "_artistUid"
}
答案 0 :(得分:0)
这个问题的全部范围对我来说还不清楚,所以我可能没有完全理解所要询问的内容,所以让我尝试回答这个问题:
如何查询列表属性中没有任何对象的对象。
如果您的对象具有其他对象的list属性,例如:
class PersonClass: Object {
@objc dynamic var person_name = ""
let dogs = List<DogClass>()
}
class DogClass: Object {
@objc dynamic var dog_name = ""
}
,假设您有一个在列表中有两只狗的人,然后又有一个在列表中没有狗的人。您的目标是查询列表中没有狗的所有人。
let personResults = realm.objects(PersonClass.self).filter("dogs.@count == 0")
@count是“列表”和“结果”属性上支持的聚合表达式。