SyncSubscription List and subscript

ros

#1

How do I use SyncSubscription with subscripts? Before I was using a wrapper to essentially query a Results array with different types of realm objects. I am trying to partial sync but am not able to subscript from the SyncSubscription List to make this happen? If not possible, is it also possible to query just a certain amount of objects? Thanks!


#2

@jayb967 We do not have subscript support on SyncSubscription APIs. This is also due to how our query-based sync returns all child objects and lists that match the query. So what you could do is use the query-based sync APIs then once you have the results - use subscripts to get what you need.

What is your use case and what are you trying to do?


#3

I have a social media feed-like application with different realm objects on the same feed. I only subscript based on the fact that I am wrapping different realm objects and was the only way to take advantage of the Live Results array. Here is how i define wrapper:

open class MixedFeedObject: Object { @objc open dynamic var creationDate: Date? = Date() //sorting properties based on popularity @objc open dynamic var popularity = 1 @objc open dynamic var id: String? = "" @objc open dynamic var userId: String? = "" @objc dynamic var typeOfRealm: AnyTypeOfRealm? //Meta data primary key function returns the string from variable previously created for reference purposes override open static func primaryKey() -> String? { return "id" } }

open class TypeOfRealm: Object { @objc open dynamic var creationDate: Date? = Date() @objc open dynamic var isRepost: Bool = false @objc open dynamic var repostRefID: String? = "" }

open class AnyTypeOfRealm: Object{ @objc open dynamic var typeName: String? = "" @objc open dynamic var primaryKey: String? = "" // A list of all subclasses that this wrapper can store static let supportedClasses: [TypeOfRealm.Type] = [ UserChallengeObject.self, BrandChallengeObject.self, FeedItemObject.self, Advertisement.self ] // Construct the type-erased type of realm from any supported subclass convenience init(_ typeOfRealm: TypeOfRealm) { self.init() typeName = String(describing: type(of: typeOfRealm)) guard let primaryKeyName = type(of: typeOfRealm).primaryKey() else { fatalError("\(typeName!) does not define a primary key") } guard let primaryKeyValue = typeOfRealm.value(forKey: primaryKeyName) as? String else { fatalError("\(typeName!)'s primary key \(primaryKeyName) is not a String") } primaryKey = primaryKeyValue } // Dictionary to lookup subclass type from its name static let methodLookup: [String : TypeOfRealm.Type] = { var dict: [String : TypeOfRealm.Type] = [:] for method in supportedClasses { dict[String(describing: method)] = method } return dict }() // Use to access the *actual* TypeOfRealm value, using "as" to upcast var value: TypeOfRealm { guard let typeName = self.typeName, let type = AnyTypeOfRealm.methodLookup[typeName] else { return TypeOfRealm() } guard let value = openUserRealm()!.object(ofType: type, forPrimaryKey: primaryKey) else { print("\(typeName) with primary key \(primaryKey!) does not exist") if let realm = openUserRealm(){ try! realm.write { realm.delete(realm.objects(AnyTypeOfRealm.self).filter("primaryKey == %@", primaryKey!).first!) } } return TypeOfRealm() } return value } }

and the way I call the results in the tableView protocol:

let results = openUserRealm()?.objects(MixedFeedObject.self).filter("userId IN %@ OR userId == %@", following, me.userID!).sorted(by: sortProperties)
if let post = results[indexPath.row].typeOfRealm?.value as? FeedItemObject{ "etc... return cell"}

Is there a different way I could probably approach this? I figured if I did partial sync I wouldn’t have to load all objects rather than just what i needed once this realm starts getting bigger… Thanks!


#4

@jayb967 I don’t see any any query APIs in here but what you would do is run:
let results = openUserRealm()?.objects(MixedFeedObject.self).filter("userId IN %@ OR userId == %@", following, me.userID!).sorted(by: sortProperties)

Which would return an empty results set, then you run:
the run results.subscribe()

Then you could run:
if let post = results[indexPath.row].typeOfRealm?.value as? FeedItemObject{ "etc... return cell"}

Which should give you what you are looking for.