Collections - GetStream/stream-swift GitHub Wiki
Adding collection entries
Setup a class Food
of CollectionObject
final class Food: CollectionObject {
private enum CodingKeys: String, CodingKey {
case name
case rating
}
var name: String
var rating: Float
init(name: String, rating: Float, id: String? = nil) {
self.name = name
self.rating = rating
// For example, set the collection name here for all instances of Food.
super.init(collectionName: "food", id: id)
}
required init(from decoder: Decoder) throws {
let dataContainer = try decoder.container(keyedBy: DataCodingKeys.self)
let container = try dataContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
name = try container.decode(String.self, forKey: .name)
rating = try container.decode(Float.self, forKey: .rating)
try super.init(from: decoder)
}
override func encode(to encoder: Encoder) throws {
var dataContainer = encoder.container(keyedBy: DataCodingKeys.self)
var container = dataContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
try container.encode(name, forKey: .name)
try container.encode(rating, forKey: .rating)
try super.encode(to: encoder)
}
}
Client.shared.add(collectionObject: Food(name: "Cheese Burger", rating: 4, id: "cheese-burger")) { result in /* ... */ }
// if you don't have an id on your side, just use nil as the ID and Stream will generate a unique ID
Client.shared.add(collectionObject: Food(name: "Cheese Burger", rating: 4)) { result in /* ... */ }
Retrieving collection entries
Client.shared.get(typeOf: Food.self, collectionName: "food", collectionObjectId: "cheese-burger") { result in /* ... */ }
Deleting collection entries
Client.shared.delete(collectionName: "food", collectionObjectId: "cheese-burger") { result in /* ... */ }
Updating collection entries
Client.shared.update(collectionObject: Food(name: "Cheese Burger", rating: 1, id: "cheese-burger")) { result in /* ... */ }
Enrichment of collection entries
// first we add our object to the food collection
let cheeseBurger = Food(name: "Cheese Burger", rating: 4, id: "cheese-burger")
// setup an enriched activity type
typealias UserFoodActivity = EnrichedActivity<User, Food, String>
Client.shared.add(collectionObject: cheeseBurger) { _ in
// the object returned by .add can be embedded directly inside of an activity
userFeed.add(UserFoodActivity(actor: Client.shared.currentUser!, verb: "grill", object: cheeseBurger)) { _ in
// if we now read the feed, the activity we just added will include the entire full object
userFeed.get(typeOf: UserFoodActivity.self) { result in
let activities = try! result.get().results
// we can then update the object and Stream will propagate the change to all activities
cheeseBurger.name = "Amazing Cheese Burger"
Client.shared.update(collectionObject: cheeseBurger) { result in /* ... */ }
}
}
}
References
// First create a collection entry with upsert api
let cheeseBurger = Food(name: "Cheese Burger", rating: 4, id: "cheese-burger")
Client.shared.add(collectionObject: cheeseBurger) { _ in
// Then create a user
let user = User(id: "john-doe")
Client.shared.create(user: user) { _ in
// Since we know their IDs we can create references to both without reading from APIs
// The `CollectionObjectProtocol` and `UserProtocol` conformed to the `Enrichable` protocol.
let cheeseBurgerRef = cheeseBurger.referenceId
let johnDoeRef = user.referenceId
Client.shared.flatFeed(feedSlug: "user", userId: "john")
.add(Activity(actor: johnDoeRef, verb: "eat", object: cheeseBurgerRef)) { result in /* ... */ }
}
}