在我深入研究我的问题之前.我的目标可能会影响您的答案,Object如果数据不再存在于云中,则会删除数据.
所以,如果我有一个数组 ["one", "two", "three"]
然后在我的服务器中删除 "two"
我希望我的领域能够更新变化.
我认为最好的方法是删除特定的所有数据Object,然后调用我的REST API下载新数据.如果有更好的方法,请告诉我.
好的,这是我的问题.
我有一个对象 Notifications()
每次调用我的REST API时,在下载任何我正在运行的内容之前:
let realm = Realm()
let notifications = Notifications()
realm.beginWrite()
realm.delete(notifications)
realm.commitWrite()
Run Code Online (Sandbox Code Playgroud)
运行后我收到此错误: Can only delete an object from the Realm it belongs to.
所以我尝试过这样的事情:
for notification in notifications {
realm.delete(notification)
}
realm.commitWrite()
Run Code Online (Sandbox Code Playgroud)
我在xcode中得到的错误是这样的: "Type Notifications does not conform to protocol 'SequenceType'
不确定从哪里开始.
只是想弄清楚领域.完全是新的
注意:realm.deleteAll()有效,但我不希望我的所有领域都被删除,只是确定Objects
我试过搜索Realm,但以下问题没有得到解答.请帮助我正确理解以下问题:
领域是关系数据库吗?
与SQLite相比,它的效率如何?
我有一个对象有许多狗的人.应用程序有单独的页面,它只显示狗和其他页面显示人的狗
我的模型如下
class Person: Object {
dynamic var id = 0
let dogs= List<Dog>()
override static func primaryKey() -> String? {
return "id"
}
}
class Dog: Object {
dynamic var id = 0
dynamic var name = ""
override static func primaryKey() -> String? {
return "id"
}
}
Run Code Online (Sandbox Code Playgroud)
我有人存储在Realm中.人有详细页面,我们取,并显示他的狗.如果狗已经存在,我会更新该狗的最新信息并将其添加到人的狗列表中,否则创建新狗,保存并将其添加到人员列表中.这适用于coredata.
// Fetch and parse dogs
if let person = realm.objects(Person.self).filter("id =\(personID)").first {
for (_, dict): (String, JSON) in response {
// Create dog using the dict info,my custom init …Run Code Online (Sandbox Code Playgroud) 在添加Realm库之前,我的apk的初始大小是2.3 MB,添加相同后,apk大小增加到10.61 MB,是否可以减小大小,如果是的话怎么样?如果没有,那么请推荐Realm的替代品
我有下课
public class Student extends RealmObject{
private int studentID;
private String studentName;
// getters and setters here
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将值设置为已创建的学生对象
student.setStudentName("Peter");
Run Code Online (Sandbox Code Playgroud)
然后我得到以下错误
java.lang.IllegalStateException:读取事务期间的可变方法调用.
为了克服这个问题,我必须按照以下方式做到这一点
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
student.setStudentName("Peter");
realm.commitTransaction();
Run Code Online (Sandbox Code Playgroud)
我不想在数据库中保留此更改.如何在不将数据库持久保存到数据库的情况下,将值设置/更改为域对象变量?
有谁知道如何使用Realm存储字符串数组?我正在尝试将以下响应映射到Realm中:
"zoneInfo": {
"tariffInfoLines": [
"In this city you pay per minute."
]
}
Run Code Online (Sandbox Code Playgroud)
我们有一个包含tariffInfoLines数组的zoneInfo对象.此tariffInfoLines数组包含字符串.在Realm中,有两种不同的变量类型用于存储数据.第一个是RLMObject,它允许你的标准NSString,int,long等.
第二种类型是RLMArray,用于数组(不支持NSArray).您必须为数组提供一个类型,该类型必须是RLMObject的子类.到目前为止,我们通过使用ABCRealmString对象解决了这个问题,如下所示:
@property RLMArray<ABCRealmString> *tariffInfoLines;
Run Code Online (Sandbox Code Playgroud)
ABCRealmString包含一个NSString属性(它基本上是一个包装器):
@property NSString *value;
Run Code Online (Sandbox Code Playgroud)
然而,这意味着当Realm尝试映射响应以持久化数据时,它正在寻找键"值"(属性的名称)的值.它似乎期望响应类似于以下内容:
"zoneInfo": {
"tariffInfoLines": [
{
"value": "In this city you pay per minute."
},
]
}
Run Code Online (Sandbox Code Playgroud)
在项目中,我们让它适用于以下结构:
"userOptions": [
{
"wantsEmailNotifications": true,
"wantsPushNotifications": false
},
]
Run Code Online (Sandbox Code Playgroud)
这有一个数组,其中的对象具有Realm可以映射到的清晰键值对.zoneInfo结构似乎是我们拥有一个包含值集的数组的唯一位置,而不是它们在对象内或具有任何键.
如果有人可以对此有所了解,关于是否可以使用Realm,或者是否需要API更改来匹配Realm可以映射的结构.
在iOS上,我们可以轻松调用realm.deleteAllObjects();以删除Realm数据库中的所有对象.
我们如何在Android中实现同样的目标?
我在Android中使用Realm进行本地存储.我正在获得以下响应表单服务器.
[{
"ListId": 10,
"Names": ["Name1", "Name2", "Name3", "Name4"]
}]
Run Code Online (Sandbox Code Playgroud)
这是我的模特
public class Model extends RealmObject {
private int ListId;
private RealmList<String> Names = new RealmList<String>()
public int getListId() {
return ListId;
}
public void setListId(int listId) {
ListId = listId;
}
public RealmList<String> getNames() {
return Names;
}
public void setNames(RealmList<String> names) {
Names = names;
}
}
Run Code Online (Sandbox Code Playgroud)
而我正在为ArrayList得到这个
类型参数'java.lang.String'不在其范围内; 应该扩展'io.realm.RealmObject'.
谢谢.
在我的Android项目中,我使用realm作为我的数据存储引擎.我喜欢它!
我也使用RxJava,因为它使"线程化"变得如此简单,我真的很喜欢整个"反应性思维".我喜欢它!
我使用MVP模式+一些"清洁架构"的想法来构建我的应用程序.
我Interactors是唯一知道的人Realm.我在Observable的帮助下公开数据,如下所示:
@Override
public Observable<City> getHomeTown() {
final Realm realm = Realm.getDefaultInstance();
return realm.where(City.class).equalTo("name", "Cluj-Napoca").findAllAsync().asObservable()
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
realm.close();
}
})
.compose(new NullIfNoRealmObject<City>());
}
Run Code Online (Sandbox Code Playgroud)
问题是我doOnUnsubscribe的副作用被调用之前Realm可以做它的事情,处理暴露的observable:
Caused by: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
at io.realm.BaseRealm.checkIfValid(BaseRealm.java:344)
at io.realm.RealmResults.removeChangeListener(RealmResults.java:818)
at io.realm.rx.RealmObservableFactory$3$2.call(RealmObservableFactory.java:137)
at rx.subscriptions.BooleanSubscription.unsubscribe(BooleanSubscription.java:71)
at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
at rx.Subscriber.unsubscribe(Subscriber.java:98)
at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
at rx.Subscriber.unsubscribe(Subscriber.java:98)
at rx.subscriptions.CompositeSubscription.unsubscribeFromAll(CompositeSubscription.java:150)
at …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用BUCK和Realm pod.
我将我的buck文件设置为:
apple_pod_lib(
name = "Realm",
visibility = ["PUBLIC"],
exported_headers = glob([
"Realm/**/*.h",
"Realm/**/*.hpp",
]),
srcs = glob([
"Realm/**/.{m,mm,cpp}",
]),
)
apple_pod_lib(
name = "RealmSwift",
visibility = ["PUBLIC"],
swift_version = "4",
deps = [
"//Pods:Realm"
],
srcs = glob([
"RealmSwift/**/*.swift",
]),
)
Run Code Online (Sandbox Code Playgroud)
使用Airbnb的pod宏.
但是我无法构建我的项目,因为这失败了
In target '//Pods:Realm', 'Realm/history.hpp' maps to the following header files:
- /BuckSample/Pods/Realm/include/core/realm/sync/history.hpp
- /BuckSample/Pods/Realm/include/core/realm/history.hpp
Please rename one of them or export one of them to a different path.
Run Code Online (Sandbox Code Playgroud)
我也尝试手动指定要包含的文件和标题,从那些repos查看PodSpec,但我无法使它工作,因为我当时缺少一些文件,以便在Xcode中编译项目.
realm ×10
android ×5
ios ×3
java ×2
swift ×2
buck ×1
c++ ×1
cocoapods ×1
database ×1
mvp ×1
objective-c ×1
persistence ×1
realm-list ×1
rx-java ×1