Collections

Collections enable you to store information to Stream. This allows you to use it inside your feeds, and to provide additional data for the personalized endpoints. Examples include products and articles, but any unstructured object (e.g. JSON) is a good match for collections.

Collection entries can be embedded inside activities and used to store nested data inside activities. When doing so, Stream will automatically enrich your activities with the current version of the data (see later section). Collection endpoints can be used both client-side and server-side except the batch methods that are only available server-side.

Adding Collections

This method allows you to create a new entry on a named collection.

Parameters

nametypedescriptiondefaultoptional
collectionstringThe name of the collection-
entry_idstringThe id of the entry, if not given an ID will be generated by Stream.-
dataobjectThe data related to the user-
// create a new collection object type with custom properties
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.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.add(collectionObject: Food(name: "Cheese Burger", rating: 4)) { result in /* ... */ }

The max size of a collection can not exceed 50kB

Retrieving Collections

After adding an entry to a collection, you can also retrieve it easily by its ID.

Parameters

nametypedescriptiondefaultoptional
collectionstringThe name of the collection-
entry_idstringThe id of the entry-
client.get(typeOf: Food.self, collectionName: "food", collectionObjectId: "cheese-burger") { result in /* ... */ }

Removing Collections

An entry can also be removed from a collection with its ID.

Parameters

nametypedescriptiondefaultoptional
collectionstringThe name of the collection-
entry_idstringThe id of the entry-
client.delete(collectionName: "food", collectionObjectId: "cheese-burger") { result in /* ... */ }

When you delete an entry from a collection any references will be converted to a missing reference error when reading feeds with enrichment.

Updating Collections

A collection’s entry data can be updated. Updates are propagated instantly to all activities embedding the entry.

Parameters

nametypedescriptiondefaultoptional
collectionstringThe name of the collection-
entry_idstringThe id of the entry-
dataobjectThe data related to the user-
client.update(collectionObject: Food(name: "Cheese Burger", rating: 1, id: "cheese-burger")) { result in /* ... */ }
© Getstream.io, Inc. All Rights Reserved.