A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/swift/model-data/supported-types/ below:

Supported Types - Swift SDK - Atlas Device SDKs

Realm has several types to represent groups of objects, which we call collections. A collection is an object that contains zero or more instances of one Realm type. Realm collections are homogenous: all objects in a collection are of the same type.

You can filter and sort any collection using Realm's query engine. Collections are live, so they always reflect the current state of the realm instance on the current thread. You can also listen for changes in the collection by subscribing to collection notifications.

All collection types conform to the RealmCollection protocol. This protocol inherits from CollectionType , so you can use a Realm collection as you would any other standard library collections.

Using the RealmCollection protocol, you can write generic code that can operate on any Realm collection:

func operateOn<C: RealmCollection>(collection: C) {        print("operating on collection containing \(collection.count) objects")}

The Swift SDK Results collection is a class representing objects retrieved from queries. A Results collection represents the lazily-evaluated results of a query operation. Results are immutable: you cannot add or remove elements to or from the results collection. Results have an associated query that determines their contents.

The Swift SDK also provides SectionedResults, a type-safe collection which holds ResultsSection as its elements. Each ResultSection is a results collection that contains only objects that belong to a given section key.

For example, an app that includes a contact list might use SectionedResults to display a list of contacts divided into sections, where each section contains all the contacts whose first name starts with the given letter. The ResultsSection whose key is "L" would contain "Larry", "Liam", and "Lisa".

The Swift SDK also offers several collection types you can use as properties in your data model:

  1. List, a class representing to-many relationships in models.

  2. LinkingObjects, a class representing inverse relationships in models.

  3. MutableSet, a class representing a to-many relationship.

  4. Map, a class representing an associative array of key-value pairs with unique keys.

  5. AnyRealmCollection, a type-erased class that can forward calls to a concrete Realm collection like Results, List or LinkingObjects.

Like live objects, Realm collections are usually live:

There are two cases when a collection is not live:

Combined with collection notifications, live collections enable clean, reactive code. For example, suppose your view displays the results of a query. You can keep a reference to the results collection in your view class, then read the results collection as needed without having to refresh it or validate that it is up-to-date.

Important Results indexes may change

Since results update themselves automatically, do not store the positional index of an object in the collection or the count of objects in a collection. The stored index or count value could be outdated by the time you use it.

You can use the following types to define your object model properties.

To learn how specific data types are mapped to BSON types in an App Services Schema, refer to Data Model Mapping in the Atlas App Services documentation.

See also Model Data with Device Sync - Swift SDK.

Changed in version 10.10.0: @Persisted property declaration syntax

Type

Required

Optional

Bool

@Persisted var boolName: Bool
@Persisted var optBoolName: Bool?

Int, Int8, Int16, Int32, Int64

@Persisted var intName: Int
@Persisted var optIntName: Int?

Float

@Persisted var floatName: Float
@Persisted var optFloatName: Float?

Double

@Persisted var doubleName: Double
@Persisted var optDoubleName: Double?

String

@Persisted var stringName: String
@Persisted var optStringName: String?

Data

@Persisted var dataName: Data
@Persisted var optDataName: Data?

Date

@Persisted var dateName: Date
@Persisted var optDateName: Date?

Decimal128

@Persisted var decimalName: Decimal128
@Persisted var optDecimalName: Decimal128?

UUID

@Persisted var uuidName: UUID
@Persisted var optUuidName: UUID?

ObjectId

@Persisted var objectIdName: ObjectId
@Persisted var optObjectIdName: ObjectId?

List

@Persisted var listName: List<MyCustomObjectType>

N/A

MutableSet

@Persisted var mutableSetName: MutableSet<String>

N/A

Map

@Persisted var mapName: Map<String, String>

N/A

AnyRealmValue

@Persisted var anyRealmValueName: AnyRealmValue

N/A

User-defined Object

N/A

@Persisted var optObjectPropertyName: MyCustomObjectType?

User-defined EmbeddedObject

N/A

@Persisted var optEmbeddedObjectPropertyName: MyEmbeddedObjectType?

User-defined Enums

@Persisted var enumName: MyPersistableEnum
@Persisted var optEnumName: MyPersistableEnum?

CGFloat properties are discouraged, as the type is not platform independent.

To use Key-Value Coding with a user-defined object in the @Persisted syntax, add the @objc attribute: @Persisted @objc var myObject: MyClass?

With the @Persisted property declaration syntax, you may see a performance impact when setting default values for:

@Persisted var listProperty: List<Int> and @Persisted var listProperty = List<Int>() are both valid, and are functionally equivalent. However, the second declaration will result in poorer performance.

This is because the List is created when the parent object is created, rather than lazily as needed. For most types, this is a difference so small you can't measure it. For the types listed here, you may see a performance impact when using the second declaration style.

Type

Required

Optional

Boolean

@property BOOL boolName;
@property NSNumber<RLMBool> *optBoolName;

Integer

@property int intName;
@property NSNumber<RLMInt> *optIntName;

Float

@property float floatName;
@property NSNumber<RLMFloat> *optFloatName;

Double

@property double doubleName;
@property NSNumber<RLMDouble> *optDoubleName;

String

@property NSString *stringName;
@property NSString *optStringName;

Data

@property NSData *dataName;
@property NSData *optDataName;

Date

@property NSDate *dateName;
@property NSDate *optDateName;

Decimal128

@property RLMDecimal128 *decimalName;
@property RLMDecimal128 *optDecimalName;

NSUUID

@property NSUUID *uuidName;
@property NSUUID *optUuidName;

RLMObjectId

@property RLMObjectId *objectIdName;
@property RLMObjectId *optObjectIdName;

RLMArray

@property RLMArray<MyObject *><MyObject> *arrayName;

N/A

RLMSet

@property RLMSet<RLMString> *setName;

N/A

RLMDictionary

@property RLMDictionary<NSString *, NSString *><RLMString, RLMString> *dictionaryName;

N/A

User-defined RLMObject

N/A

@property MyObject *optObjectPropertyName;

User-defined RLMEmbeddedObject

N/A

@property MyEmbeddedObject *optEmbeddedObjectPropertyName;

Additionally:

CGFloat properties are discouraged, as the type is not platform independent.

Changed in version 10.8.0: RealmProperty replaces RealmOptional

Type

Required

Optional

Bool

@objc dynamic var value = false
let value = RealmProperty<Bool?>()

Int, Int8, Int16, Int32, Int64

@objc dynamic var value = 0
let value = RealmProperty<Int?>()

Float

@objc dynamic var value: Float = 0.0
let value = RealmProperty<Float?>()

Double

@objc dynamic var value: Double = 0.0
let value = RealmProperty<Double?>()

String

@objc dynamic var value = ""
@objc dynamic var value: String? = nil

Data

@objc dynamic var value = Data()
@objc dynamic var value: Data? = nil

Date

@objc dynamic var value = Date()
@objc dynamic var value: Date? = nil

Decimal128

@objc dynamic var decimal: Decimal128 = 0
@objc dynamic var decimal: Decimal128?

UUID

@objc dynamic var uuid = UUID()
@objc dynamic var uuidOpt: UUID?

ObjectId

@objc dynamic var objectId = ObjectId.generate()
@objc dynamic var objectId: ObjectId?

List

let value = List<Type>()

MutableSet

let value = MutableSet<Type>()

Map

let value = Map<String, String>()

AnyRealmValue

let value = RealmProperty<AnyRealmValue>()

N/A

User-defined Object

N/A

@objc dynamic var value: MyClass?

Additionally:

You can use RealmProperty <T?> to represent integers, doubles, and other types as optional.

CGFloat properties are discouraged, as the type is not platform independent.

New in version 10.8.0: UUID type

ObjectId is a MongoDB-specific 12-byte unique value. UUID is a 16-byte globally-unique value. You can index both types, and use either as a primary key.

Note

When declaring default values for @Persisted UUID or ObjectId property attributes, both of these syntax types are valid:

However, the second will result in poorer performance. This is because the latter creates a new identifier that is never used any time an object is read from the realm, while the former only creates them when needed.

@Persisted var id: ObjectId has equivalent behavior to @objc dynamic var _id = ObjectId.generate(). They both make random ObjectIds.

@Persisted var _id = ObjectId() has equivalent behavior to @objc dynamic var _id = ObjectId(). They both make zero-initialized ObjectIds.

Data and string properties cannot hold more than 16MB. To store larger amounts of data, either:

Realm throws a runtime exception if your app attempts to store more than 16MB in a single property.

To avoid size limitations and a performance impact, it is best not to store large blobs, such as image and video files, directly in a realm. Instead, save the file to a file store and keep only the location of the file and any relevant metadata in the realm.

To store a collection as a property or variable without needing to know the concrete collection type, Swift's type system requires a type-erased wrapper like AnyRealmCollection:

class ViewController {    let collection: AnyRealmCollection<MyModel>    init<C: RealmCollection & _ObjcBridgeable>(collection: C) where C.ElementType == MyModel {        self.collection = AnyRealmCollection(collection)    }}

New in version 10.8.0.

A MutableSet collection represents a to-many relationship containing distinct values. A MutableSet supports the following types (and their optional versions):

Like Swift's Set , MutableSet is a generic type that is parameterized on the type it stores. Unlike native Swift collections , Realm mutable sets are reference types, as opposed to value types (structs).

You can only call the MutableSets mutation methods during a write transaction. As a result, MutableSets are immutable if you open the managing realm as a read-only realm.

You can filter and sort a MutableSet with the same predicates as Results. Like other Realm collections, you can register a change listener on a MutableSet.

For example, a Dog class model might contain a MutableSet for citiesVisited:

class Dog: Object {    @Persisted var name = ""    @Persisted var currentCity = ""    @Persisted var citiesVisited: MutableSet<String>}
Note

When declaring default values for @Persisted MutableSet property attributes, both of these syntax types is valid:

However, the second will result in significantly worse performance. This is because the MutableSet is created when the parent object is created, rather than lazily as needed.

New in version 10.8.0.

The Map is an associative array that contains key-value pairs with unique keys.

Like Swift's Dictionary , Map is a generic type that is parameterized on its key and value types. Unlike native Swift collections , Realm Maps are reference types (classes), as opposed to value types (structs).

You can declare a Map as a property of an object:

class Dog: Object {    @Persisted var name = ""    @Persisted var currentCity = ""        @Persisted var favoriteParksByCity: Map<String, String>}

Realm disallows the use of . or $ characters in map keys. You can use percent encoding and decoding to store a map key that contains one of these disallowed characters.

// Percent encode . or $ characters to use them in map keyslet mapKey = "New York.Brooklyn"let encodedMapKey = "New York%2EBrooklyn"
Note

When declaring default values for @Persisted Map property attributes, both of these syntax types is valid:

However, the second will result in significantly worse performance. This is because the Map is created when the parent object is created, rather than lazily as needed.

Changed in version 10.51.0: AnyRealmValue properties can hold lists or maps of mixed data.

New in version 10.8.0.

AnyRealmValue is a Realm property type that can hold different data types. Supported AnyRealmValue data types include:

AnyRealmValue cannot hold a MutableSet or embedded object.

This mixed data type is indexable, but you can't use it as a primary key. Because null is a permitted value, you can't declare an AnyRealmValue as optional.

class Dog: Object {    @Persisted var name = ""    @Persisted var currentCity = ""    @Persisted var companion: AnyRealmValue}

In version 10.51.0 and later, a AnyRealmValue data type can contain collections (a list or map, but not a set) of AnyRealmValue elements. You can use mixed collections to model unstructured or variable data. For more information, refer to Define Unstructured Data.

To use mixed collections in your app, define the AnyRealmValue type property in your data model. Then, you can create the list or map collections like any other mixed data value.

New in version 10.47.0.

Geospatial data, or "geodata", specifies points and geometric objects on the Earth's surface.

If you want to persist geospatial data, it must conform to the GeoJSON spec.

To persist geospatial data with the Swift SDK, create a GeoJSON-compatible embedded class that you can use in your data model.

Your custom embedded object must contain the two fields required by the GeoJSON spec:

class CustomGeoPoint: EmbeddedObject {    @Persisted private var type: String = "Point"    @Persisted private var coordinates: List<Double>    public var latitude: Double { return coordinates[1] }    public var longitude: Double { return coordinates[0] }    convenience init(_ latitude: Double, _ longitude: Double) {        self.init()                coordinates.append(objectsIn: [longitude, latitude])    }}

New in version 10.20.0.

You can use Type Projection to persist unsupported types as supported types in Realm. This enables you to work with Swift types that Realm does not support, but store them as types that Realm does support. You could store a URL as a String, for example, but read it from Realm and use it in your application as though it were a URL.

To use type projection with Realm:

  1. Use one of Realm's custom type protocols to map an unsupported data type to a type that Realm supports

  2. Use the projected types as @Persisted properties in the Realm object model

You can map an unsupported data type to a type that Realm supports using one of the Realm type projection protocols.

The Swift SDK provides two type projection protocols:

Use CustomPersistable when there is no chance the conversion can fail.

Use FailableCustomPersistable when it is possible for the conversion to fail.

extension CLLocationCoordinate2D: CustomPersistable {                public typealias PersistedType = Location            public init(persistedValue: PersistedType) {        self.init(latitude: persistedValue.latitude, longitude: persistedValue.longitude)    }            public var persistableValue: PersistedType {        Location(value: [self.latitude, self.longitude])    }}extension URL: FailableCustomPersistable {            public typealias PersistedType = String                public init?(persistedValue: String) { self.init(string: persistedValue) }            public var persistableValue: String { self.absoluteString }}

The PersistedType can use any of the primitive types that the Swift SDK supports. It can also be an Embedded Object.

PersistedType cannot be an optional or a collection. However you can use the mapped type as an optional or collection property in your object model.

extension URL: FailableCustomPersistable {         public typealias PersistedType = String?   ...}class Club: Object {   @Persisted var id: ObjectId   @Persisted var name: String         @Persisted var url: URL?}

A type that conforms to one of the type projection protocols can be used with the @Persisted property declaration syntax introduced in Swift SDK version 10.10.0. It does not work with the @objc dynamic syntax.

You can use projected types for:

When using a FailableCustomPersistable as a property, define it as an optional property. When it is optional, the FailableCustomPersistable protocol maps invalid values to nil. When it is a required property, it is force-unwrapped. If you have a value that can't be converted to the projected type, reading that property throws an unwrapped fail exception.

class Club: Object {    @Persisted var id: ObjectId    @Persisted var name: String            @Persisted var url: URL?                        @Persisted var location: CLLocationCoordinate2D}public class Location: EmbeddedObject {    @Persisted var latitude: Double    @Persisted var longitude: Double}

When your model contains projected types, you can create the object with values using the persisted type, or by assigning to the field properties of an initialized object using the projected types.

let club = Club(value: ["name": "American Kennel Club", "url": "https://akc.org"])let club2 = Club()club2.name = "Continental Kennel Club"club2.url = URL(string: "https://ckcusa.com/")!club2.location = CLLocationCoordinate2D(latitude: 40.7509, longitude: 73.9777)

When you declare your type as conforming to a type projection protocol, you specify the type that should be persisted in realm. For example, if you map a custom type URL to a persisted type of String, a URL property appears as a String in the schema, and dynamic access to the property acts on strings.

The schema does not directly represent mapped types. Changing a property from its persisted type to its mapped type, or vice versa, does not require a migration.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4