DittoQueryResultItem

public class DittoQueryResultItem

Represents a single match of a DQL query, similar to a “row” in SQL terms. It’s a reference type serving as a “cursor”, allowing for efficient access of the underlying data in various formats.

The value property is lazily materialized and kept in memory until it goes out of scope. To reduce the memory footprint, structure your code such that items can be processed as a stream, i.e. one by one (or in batches) and dematerialize() them right after use:

let cars = allCarsQueryResult.items.map { item in
    // Pull out relevant data:
    let model = item.value["model"] as! String
    let mileage = item.value["mileage"] as! Int

    // Prune to free memory:
    item.dematerialize()

    // Use the pulled out data:
    return Car(model: model, mileage: mileage)
}

Pulling the contents out of a dictionary can be very cumbersome in Swift, especially if your data is deeply nested. Consider defining a struct describing the shape of your data, then decoding it via jsonString():

struct Car: Codable {
    model: String
    mileage: Int
}

let jsonData = item.jsonString().data(using: .utf8)
try JSONDecoder().decode(Car.self, data: jsonData)
  • Returns the content as a materialized dictionary.

    The item’s value is materialize()-ed on first access and subsequently on each access after performing dematerialize(). Once materialized, the value is kept in memory until explicitly dematerialize()-ed or the item goes out of scope.

    Note

    This property is very similar to DittoDocument.value.

    Declaration

    Swift

    public var value: Dictionary<String, Any?> { get }
  • Returns true if value is currently held materialized in memory, otherwise returns false.

    See Also

    Declaration

    Swift

    public var isMaterialized: Bool { get }
  • Loads the CBOR representation of the item’s content, decodes it as a dictionary so it can be accessed via value. Keeps the dictionary in memory until dematerialize() is called. No-op if value is already materialized.

    Declaration

    Swift

    public func materialize()
  • Releases the materialized value from memory. No-op if item is not materialized.

    Declaration

    Swift

    public func dematerialize()
  • Returns the content of the item as CBOR data.

    Important

    The returned CBOR data is not cached, make sure to call this method once and keep the returned data for as long as needed.

    Declaration

    Swift

    public func cborData() -> Data
  • Returns the content of the item as JSON data containing a UTF-8 encoded string.

    Important

    The returned JSON data is not cached, make sure to call this method once and keep returned data for as long as needed.

    Declaration

    Swift

    public func jsonData() -> Data
  • Returns the content of the item as a JSON string.

    Important

    The returned JSON string is not cached, make sure to call this method once and keep returned data for as long as needed.

    Declaration

    Swift

    public func jsonString() -> String