我试图将Realm与Snappydb进行比较(对于那些想要查看基准测试的人来说,这是我的回购).我想我的方式是错误的,因为与Sanppydb相比,存储到数据库的时间在Realm中需要超长的时间.
基准测试显示以下结果.正如您在图像中看到的那样,Realm比Snappydb慢大约100-200倍.

我正在做的是先创建10,000个对象,然后将它们存储到数据库中.因此,在我的代码中,我以这种方式存储一个Booking对象(有一个for循环,迭代10,000次):
public void storeBooking(final Booking booking) {
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Booking realmBooking = realm.createObject(Booking.class);
realmBooking.setId(booking.getId());
realmBooking.setCode(booking.getCode());
realmBooking.setFareLowerBound(booking.getFareLowerBound());
realmBooking.setFareUpperBound(booking.getFareUpperBound());
realmBooking.setPhoneNumber(booking.getPhoneNumber());
realmBooking.setPickUpTime(booking.getPickUpTime());
realmBooking.setRequestedTaxiTypeName(booking.getRequestedTaxiTypeName());
realmBooking.setTaxiTypeId(booking.getTaxiTypeId());
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是用于存储Booking对象的Snappydb方法.
public void storeBooking(final String key, final Booking booking) {
try {
mSnappyDb.put(key, booking);
} catch (SnappydbException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
使用insertOrUpdate()方法和一个事务的新结果
我有以下Swift 4 Codable类,它继承自Realm的Object类型:
final class SearchResult: RealmSwift.Object, Codable {
@objc dynamic var name: String = ""
@objc dynamic var region: String = ""
enum CodingKeys: String, CodingKey {
case name = "name"
case region = "region"
}
}
Run Code Online (Sandbox Code Playgroud)
这里的期望是该init(from decoder: Decoder)方法被合成,所以我不必自己实现它,这是一个巨大的便利.但是,不实现这会产生以下编译器错误:
super.init isn't called on all paths before returning from initializer
有三种方法可以摆脱编译器错误,但没有一种方法是好的:
实现一个init(from decoder: Decoder)只调用的空方法super.init().这似乎阻止了合成,意味着没有任何实际解码,因为它只是一个空方法.
init(from decoder: Decoder)手动实现整个方法.这有效,但现在使用的乐趣Codable几乎消失了.
删除所有与Realm相关的代码.现在Codable按预期工作,但是,现在我不能再使用Realm了.
这对我来说似乎是一个Swift错误,因为它应该检测到init(from decoder: Decoder)实际上是在实现,而不是手动执行.
我不知道的任何建议或解决方法?
所以我不知道为什么我会收到这个错误.错误消息如下:
*由于未捕获的异常'RLMException'而终止应用程序,原因是:'尝试修改写事务之外的对象 - 首先在RLMRealm实例上调用beginWriteTransaction.*第一次抛出调用堆栈:(0x2f7b0f83 0x39f61ccf 0xc46ef 0xc3c23 0xc0c9d 0xb3e73 0x3a449833 0x3a449ded 0x3a44a297 0x3a45c88d 0x3a45cb21 0x3a58bbd3 0x3a58ba98)libc ++ abi.dylib:以NSException类型的未捕获异常终止
并在执行此代码时抛出.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField * alertTextField = [alertView textFieldAtIndex:0];
if (![self.chatSession.theirAlias isEqualToString:alertTextField.text]) {
self.sender = alertTextField.text;
dispatch_queue_t queue = ((AppDelegate *)[UIApplication sharedApplication].delegate).queueForWrites;
dispatch_async(queue, ^{
[[RLMRealm defaultRealm] beginWriteTransaction];
self.chatSession.myAlias = alertTextField.text; // This is the line where the error is thrown
[[RLMRealm defaultRealm] commitWriteTransaction];
});
} else {
[self promptForAliasAfterRejection];
}
}
Run Code Online (Sandbox Code Playgroud)
我很清楚我在写一个事务中写作.这是Realm的错误吗?或者我错过了什么......?
我在Android Studio中制作了一个使用两个库的应用程序.一个带有Android包装器和jar库的本机库.出于某种原因,如果将其他jar库编译到项目中,则不会加载本机库.因此,如果我只使用本机库运行应用程序,一切正常.我将另一个jar库添加到我的gradle文件并繁荣...一个UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError: Couldn't load MobileOcrEngine from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.app-1, /vendor/lib, /system/lib]]]: findLibrary returned null
Run Code Online (Sandbox Code Playgroud)
我用这个时我的应用运行正常:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
compile 'com.android.support:support-v13:21.0.2'
compile project(':wheel')
}
Run Code Online (Sandbox Code Playgroud)
我尝试时发生错误:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
compile 'com.android.support:support-v13:21.0.2'
compile project(':wheel')
compile files('libs/realm-0.78.0.jar')
}
Run Code Online (Sandbox Code Playgroud)
或者当我尝试使用相同的库但使用Maven存储库时:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
compile 'com.android.support:support-v13:21.0.2'
compile project(':wheel')
compile 'io.realm:realm-android:0.78.0'
}
Run Code Online (Sandbox Code Playgroud)
或者如果我尝试将jar放在jniLibs文件夹中:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
compile 'com.android.support:support-v13:21.0.2'
compile project(':wheel')
}
Run Code Online (Sandbox Code Playgroud)
我不知道问题的根源在哪里.有两个库中的一个,Android Studio还是我做错了什么?
注意: 我知道StackOverflow上有很多关于UnsatisfiedLinkErrors的问题,但这些都没有为我的问题提供解决方案.如果它是我使用的唯一库,我加载本机库没有问题...
我有一个Realm对象类,并在那里存储大量数据,想象我有一个 String uid; 字段.我想得到uid的名字,但在同一个uid名称上只有一次,例如
AA
AA
BB
CC
DD
BB
BB
我想得到AA,
BB,
CC,
DD.
只有一次.我查看了领域文档,但找不到任何东西.
谢谢你的回答.
可以说我有以下场景......
ViewController1Person从主线程上的Realm 加载一个对象并将其传递给ViewController2.用户交互ViewController2导致同一个Person对象发生变化,但我只想在用户按下"保存"后保留更改.
目前,当Person在ViewController2运行时更改传递的对象时,会抛出错误,表示需要在Write块中对对象进行更改.这是有道理的,但在这种情况下,我实际上并不想立即坚持更改.
我刚开始使用Realm for Swift.阅读完文档后,我仍然记得几个问号.
我最大的一个是,如果有一个从Realm类中分离Model类的最佳实践.
例如,在Java MVC项目中,我们使用了DAO类(数据访问对象类),它们负责与数据库层的通信.我们相应的模型类只有注入的dao对象或我们使用的服务类(如CRUD操作).
如果我是一个领域"模型"类,现在看起来似乎是一切.但是在将对象持久化到数据库之后,更改UI-Layer中对象的属性会导致
'尝试修改写事务之外的对象 - 首先在RLMRealm实例上调用beginWriteTransaction.
这让我回到了我的初期:不应该在Realm对象和模型对象中分开.或者可以在View-Classes中使用"realm.write"进程吗?
我对此做了一些研究,但结果非常有效.
你如何在你的项目中处理这个问题.你有某种最佳实践或指导吗?
非常感谢约翰
我正在尝试通过Cocoapods安装Realm for Swift.
首先我做的是pod init进入我的项目
然后我打开podfile并改变它:
target 'Taskio' do
use_frameworks!
pod 'RealmSwift'
end
Run Code Online (Sandbox Code Playgroud)
然后我关闭podfile并执行命令pod install
一切都顺利通过.但是现在当我打开工作区时,我在导入RealmSwift时遇到错误
无法为'RealmSwift'加载底层模块
我有一个从Web上抓取的数据文件,它是使用python脚本生成的.
如何将其加载到我的Realm反应本机数据库中?
我偶然发现了可以从JSON文件加载而没有任何修改的想法,是否可能,如何做到这一点?
如何在本机中指定文件的路径?如何解析本机中的文件?
更新:您能否建议我在React Native中使用数据的一般工作流程?我还没有找到有用的手册.
我想,我应该解析来自React native(通用脚本)的文件,在这种情况下如何指定它们的路径,或者我应该在特定平台(Andriod/iOS项目)中使用它们?