Skip to content

BellAppLab/BackgroundRealm

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

Background Realm Version License

Platforms Swift support CocoaPods Compatible Carthage compatible SwiftPM compatible Twitter

Background Realm

Background Realm is a collection of handy classes and extensions that make it easier to work with RealmSwift in the background.

It's main focus is to enhance existing Realms and Realm-based code bases with very little overhead and refactoring.

Note: Although this module makes it more convenient to work with a Realm in the background, it does not make Realms nor its objects thread-safe. They should still be accessed only from within their appropriate thread.

Specs

  • RealmSwift 10.0.0+
  • iOS 12+
  • tvOS 12+
  • watchOS 4+
  • macOS 10.13+
  • Swift 5.0+

Objective-C

For the Objective-C counterpart, see BLBackgroundRealm.

Writing to a Realm in the background

Performing write transactions in the background becomes as easy as:

Realm.writeInBackground(configuration: <#T##Realm.Configuration?#>) { (result) in
    <#code#>
}

Optionally, you can set a default backgroundConfiguration that will be used in all write transactions in the background:

Realm.Configuration.backgroundConfiguration = <#T##Realm.Configuration?#>

Realm.writeInBackground { (result) in
    <#code#>
}

Finally, you can easily move from any Realm instance to its background counterpart:

let realm = try Realm()

realm.writeInBackground { (result) in 
    <#code#>
}

Commiting to a Realm in the background

Similarly to write operations, you can commit transactinos to a Realm in the background. The difference being that commits can be cancelled:

Realm.commitInBackground(configuration: <#T##Realm.Configuration?#>) { (result) -> Bool in
    <#code#>
    return false //return true if you want to cancel this write operation
}

You can also move from any Realm instance to its background counterpart:

let realm = try Realm()

realm.commitInBackground { (result) -> Bool in 
    <#code#>
    return false //return true if you want to cancel this write operation
}

The BackgroundRealm

Background Realm exposes a BackgroundRealm class, which basically:

  1. creates a private Thread and RunLoop where a new background Realm will be opened
  2. opens a Realm in the private thread
  3. runs work in the background thread

This is particularly useful if you'd like to:

  • make computationally expensive changes to the Realm
  • register for change notifications in the background, without necessarily triggering a UI update right away

Usage

  • Creating a BackgroundRealm using Realm.Configuration.backgroundConfiguration:
let backgroundRealm = BackgroundRealm { (result) in
    <#code#>
}
  • Creating a BackgroundRealm using a custom configuration:
let backgroundRealm = BackgroundRealm(configuration: <#T##Realm.Configuration?#>) { (result) in
    <#code#>
}
  • Creating a BackgroundRealm using a file URL:
let backgroundRealm = BackgroundRealm(fileURL: <#T##URL#>) { (result) in
    <#code#>
}

Queues

BackgroundRealm uses two queues to process things in the background:

  • DispatchQueue.backgroundRealm
  • OperationQueue.backgroundRealm

If you'd like to use your own queues, just set those as early as possible in you app's life cycle.

Installation

Cocoapods

pod 'BackgroundRealm', '~> 4.0'

Then import BackgroundRealm where needed.

Carthage

github "BellAppLab/BackgroundRealm" ~> 4.0

Then import BackgroundRealm where needed.

Swift Package Manager

.package(url: "https://github.com/BellAppLab/BackgroundRealm.git", from: "4.0.0")

Git Submodules

cd toYourProjectsFolder
git submodule add -b submodule --name BackgroundRealm https://github.com/BellAppLab/BackgroundRealm.git

Then drag the BackgroundRealm folder into your Xcode project.

Author

Bell App Lab, apps@bellapplab.com

Contributing

Check this out.

Credits

Logo image by mikicon from The Noun Project

License

BackgroundRealm is available under the MIT license. See the LICENSE file for more info.

About

A collection of handy classes and extensions that make it easier to work with RealmSwift in the background.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published