Skip to content

GeofenceManager

public classGeofenceManager

Manages the geofence database — add, remove, query, and check geofences.

Access via BGGeo.instance.geofences. All methods are suspending coroutines (Kotlin) or async/await (Swift).

Methods
Method Description
[add] Add a single geofence.
[addAll] Add multiple geofences in bulk (~10× faster than repeated [add]).
[remove] Remove a geofence by identifier.
[removeAll] Remove all geofences, or a specific subset.
[get] Fetch a single geofence by identifier.
[getAll] Fetch all stored geofences.
[exists] Check whether a geofence with a given identifier exists.

Members

add

public func add(_ geofence:BGGeo.Geofence) async throws

Add a Geofence to be monitored by the native geofencing API.

Note

If a geofence with the same Geofence.identifier already exists, it is deleted before the new one is inserted. When adding multiple geofences, addGeofences is approximately 10× faster.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
let geofence = Geofence(
    identifier: "Home",
    radius: 150,
    latitude: 45.51921926,
    longitude: -73.61678581,
    notifyOnEntry: true,
    notifyOnExit: false,
    notifyOnDwell: true,
    loiteringDelay: 30000,  // 30 seconds
    extras: ["zone_id": 1234]
)

Task {
    do {
        try await bgGeo.geofences.add(geofence)
        print("[addGeofence] success")
    } catch {
        print("[addGeofence] FAILURE: \(error)")
    }
}

addAll

public func addAll(_ geofences: [BGGeo.Geofence]) async throws

Add a list of Geofence to be monitored by the native geofencing API.

Note

If any geofence already exists with a matching Geofence.identifier, it is deleted before the new one is inserted.

See also - 📘 Geofencing Guide - addGeofence

let bgGeo = BGGeo.shared
let geofences = [
    Geofence(
        identifier: "foo",
        radius: 200,
        latitude: 45.51921926,
        longitude: -73.61678581,
        notifyOnEntry: true
    ),
    Geofence(
        identifier: "bar",
        radius: 200,
        latitude: 45.51921926,
        longitude: -73.61678581,
        notifyOnEntry: true
    )
]

Task {
    do {
        try await bgGeo.geofences.addAll(geofences)
    } catch {
        print("Error: \(error)")
    }
}

exists

public func exists(_ identifier: String) async -> Bool

Determine whether a geofence with the given identifier exists in the SDK's database.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
Task {
    let exists = await bgGeo.geofences.exists("HOME")
    print("[geofenceExists]", exists)
}

get

public func get(_ identifier: String) async throws ->BGGeo.Geofence

Fetch a single Geofence by identifier from the SDK's database.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
Task {
    do {
        let geofence = try await bgGeo.geofences.get("HOME")
        print("[getGeofence]", geofence)
    } catch {
        print("Error: \(error)")
    }
}

getAll

public func getAll() async -> [BGGeo.Geofence]

Fetch all Geofence records from the SDK's database.

Returns an empty array if no geofences are stored.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
let geofences = bgGeo.geofences.getAll()
print("[getGeofences]", geofences)

remove

public func remove(_ identifier: String) async throws

Remove the Geofence with the given Geofence.identifier.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
Task {
    do {
        try await bgGeo.geofences.remove("Home")
        print("[removeGeofence] success")
    } catch {
        print("[removeGeofence] FAILURE: \(error)")
    }
}

removeAll

public func removeAll(_ identifiers: [String]? = nil) async throws

Remove all monitored Geofence records, or a specific subset by identifier.

See also - 📘 Geofencing Guide

let bgGeo = BGGeo.shared
Task {
    try await bgGeo.geofences.removeAll()
}