BLBackgroundRealm is a collection of handy classes and extensions that make it easier to work with RLMRealm in the background.
It's main focus is to enhance existing RLMRealms and Realm-based code bases with very little overhead and refactoring.
Note: Although this module makes it more convenient to work with a RLMRealm in the background, it does not make RLMRealms nor its objects thread-safe. They should still be accessed only from within their appropriate thread.
- Realm 3.0.0+
- iOS 9+
- tvOS 10+
- watchOS 3+
- macOS 10.10+
For the Swift counterpart, see BackgroundRealm.
Commiting write transactions in the background becomes as easy as:
[RLMRealm writeInBackgroundWithConfiguration:<#(nonnull RLMRealmConfiguration *)#>
andBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error)
{
<#code#>
}];Optionally, you can set a default backgroundConfiguration that will be used in all write transactions in the background:
RLMRealmConfiguration *config = [[RLMRealmConfiguration alloc] init];
config.fileURL = url;
[RLMRealmConfiguration setBackgroundConfiguration:config];
[RLMRealm writeInBackgroundWithBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error) {
<#code#>
}];Finally, you can easily move from any Realm instance to its background counterpart:
RLMRealm *realm = [RLMRealm defaultRealm];
[realm writeInBackgroundWithBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error) {
<#code#>
}];Background Realm exposes a BLBackgroundRealm class, which basically:
- creates a private
NSThreadandNSRunLoopwhere a new backgroundRLMRealmwill be opened - opens a
RLMRealmin the private thread - runs work in the background thread
This is particularly useful if you'd like to:
- make computationally expensive changes to the
RLMRealm - register for change notifications in the background, without necessarily triggering a UI update right away
- Creating a
BLBackgroundRealmusing[RLMConfiguration backgroundConfiguration]:
BLBackgroundRealm *bgRealm = [BLBackgroundRealm backgroundRealmWithBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error) {
<#code#>
}];- Creating a
BLBackgroundRealmusing a custom configuration:
[BLBackgroundRealm backgroundRealmWithConfiguration:<#(nonnull RLMRealmConfiguration *)#>
andBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error)
{
<#code#>
}];- Creating a
BLBackgroundRealmusing a fileNSURL:
[BLBackgroundRealm backgroundRealmWithFileURL:<#(nonnull NSURL *)#>
andBlock:^(RLMRealm * _Nullable realm, BLBackgroundRealmError * _Nullable error)
{
<#code#>
}];pod 'BLBackgroundRealm', '~> 1.0'Then #import <BLBackgroundRealm/BLBackgroundRealm.h> where needed.
github "BellAppLab/BLBackgroundRealm" ~> 1.0Then #import <BLBackgroundRealm/BLBackgroundRealm.h> where needed.
cd toYourProjectsFolder
git submodule add -b submodule --name BLBackgroundRealm https://github.com/BellAppLab/BLBackgroundRealm.gitThen drag the BLBackgroundRealm folder into your Xcode project.
When forking this repo, make sure to download the Realm framework and add it manually to the project.
Bell App Lab, [email protected]
Check this out.
Logo image by mikicon from The Noun Project
BackgroundRealm is available under the MIT license. See the LICENSE file for more info.
