我想RealmObject从它"分离"一个Realm,这意味着我希望能够RealmObject从一个方法返回一个并且能够在我close的Realm实例之后使用它.
像这样的东西:
public Person getPersonWithId(final Context context, final String personId){
Realm realm = Realm.getInstance(context);
Person person = realm.where.....;
realm.close();
return person;
}
Run Code Online (Sandbox Code Playgroud)
目前getPersonWithId(mContext, personId).getName()将按预期返回错误.
拥有一个托管对象也意味着该对象是不可变的(无法修改),因此任何更新对象的方法person.setName(String name)都会因为该对象是一个托管对象而失败.
我希望会有像这样的方法 Person person = person.detachFromRealm();
有谁知道这个问题的解决方案/解决方法?
我有一个Realm Object建模如此
class WorkoutSet: Object {
// Schema 0
dynamic var exerciseName: String = ""
dynamic var reps: Int = 0
// Schema 0 + 1
dynamic var setCount: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行迁移.
在我的AppDelegate导入内RealmSwift.
在didFinishLaunchWithOptions我调用的函数内
Migrations().checkSchema()
Run Code Online (Sandbox Code Playgroud)
迁移是在另一个文件中声明的类.
在该文件中有一个声明为这样的结构.
func checkSchema() {
Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1, …Run Code Online (Sandbox Code Playgroud) 我有一个对象
public class ArticleList extends RealmObject {
@PrimaryKey
private String id;
private String title;
private String subtitle;
private String image;
private String category;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是从Realm获取结果并将结果转换为 ArticleList[]
通过使用获取我
RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "CategoryName").findAll();
Run Code Online (Sandbox Code Playgroud)
接下来我需要做什么才能获得一系列对象?
看起来RealmProxy类隐藏了我的RealmObject值,但可以从proxyclass设置.
你可以看到我的模型非常简单.
public class GroupRealm extends RealmObject {
@PrimaryKey
public String id;
@Index
public String name;
public String imageUrl;
public int order;
public GroupRealm parent;
public RealmList<GroupRealm> children;
public RealmList<ContentRealm> contents;
}
Run Code Online (Sandbox Code Playgroud)
这是我设置值的方式(db是一个有效的Realm,一切都在提交正常的事务中):
GroupRealm gr = db.where(GroupRealm.class).equalTo("id",g.GroupID).findFirst();
if(gr==null){
gr = db.createObject(GroupRealm.class,g.GroupID);
}
gr.imageUrl = g.GlyphUrl;
gr.name = g.Title;
gr.order = g.OrderNum;
Run Code Online (Sandbox Code Playgroud)
下面的图片是我查询db后面的内容.(相同的变量名称在代码中不相同)
在我的android.library中,我的RealmObjects被定义为项目,我有必要的插件.
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
Run Code Online (Sandbox Code Playgroud)
在项目级别,我正在设置正确的依赖项:
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath "io.realm:realm-gradle-plugin:0.90.1"
// NOTE: Do not place your application dependencies here; they belong
// in the …Run Code Online (Sandbox Code Playgroud) 我只是使用Realm测试一些配置,因此我在我的领域类中添加和删除了变量和列表.由于我只是在测试,我不想经历迁移过程 - 我也没有任何影响连续性的数据.
有没有办法绕过Realm自动请求的迁移?
我想知道实例化Realm数据库的最佳实践是针对Swift 2. Realm for Swift 1.2和Swift 2之间的主要区别之一是Realm类增加了对错误处理的支持.因此,Realm网站上的此代码不再起作用:
让realm = Realm()
我可以想到在Swift 2世界中实例化一个Realm类的几种方法:
(1)让领域=试试!领域()
这个选项对我来说似乎有些"不安全",因为如果类无法实例化,它可能会导致运行时错误.
(2)将整个Realm操作(包括类实例化)放在Do-Catch块中
do {
let realm = try Realm()
realm.write{realm.add(myObject)}
}
catch
{
print("Some Realm error")
}
Run Code Online (Sandbox Code Playgroud)
这绝对有效,绝对安全.但是,每次我需要在数据库上执行操作时,我真的不喜欢实例化Realm类.如果我尝试创建一个IVAR'领域'并将其放在Do-Catch块之外,则该变量超出范围.例如,以下代码将无法编译......
//IVAR declared outside of Do-Catch...
let realm:Realm
do{
//Get instance of Realm
realm = try Realm()
//This write operation works
realm.write{realm.add(myObject_1)}
}
catch
{
print("Some Realm error")
}
//Create another Dog object
let myObject_2 = SomeObject()
//This next line produces an error: "Variable 'realm' used before being initialized".
//Initialized …Run Code Online (Sandbox Code Playgroud) 我是反应原生的新领域.
我经常更改架构,但每次更改时都会出现迁移错误.
更改架构时如何重置域数据库?
我在表A last_message_time中输入Date了一个类型的列/字段.假设查询表A返回x结果.如何根据last_message_time列内的日期对这些结果进行排序.
例如,在SQLite中我们有 ORDER BY date(dateColumn)
好的,所以我发现了很多关于UITableView和多个部分的信息,但是,它们总是包含字符串,数组,静态数据,Obj-C或其他我无法转化为我的情况,主要是因为我完全是开发应用程序的新手.非常感谢任何帮助,因为我已经用了一个多月的时间尝试了不同的方法而没有成功.
所以我有多个具有以下属性的Dog对象:
class Dog: Object {
dynamic var name = ""
dynamic var race = ""
dynamic var age = 0
dynamic var owner = ""
dynamic var dogID = ""
override static func primaryKey() -> String? {
return "dogID"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的ViewController文件中,我有以下代码(我删除了不相关的行):
let realm = try! Realm()
var dogResults : Results<Dog>?
override func viewDidLoad() {
super.viewDidLoad()
self.dogResults = realm.objects(Dog.self)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dogResults!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> …Run Code Online (Sandbox Code Playgroud) 正如官方文档中所见,如何使用Realm
// Initialize Realm
Realm.init(context);
// Get a Realm instance for this thread
Realm realm = Realm.getDefaultInstance();
Run Code Online (Sandbox Code Playgroud)
我将dependencie添加到了我的项目中
classpath "io.realm:realm-gradle-plugin:2.0.2"
Run Code Online (Sandbox Code Playgroud)
我可以正常使用这个库,但静态方法init显然不存在.有人可以发布一个如何使用此库初始化并将示例对象保存到数据库的示例吗?实际上没有太多的教程,在你设法启动之后,使用起来非常简单.领域初始化设置默认配置对吗?那么有没有办法绕过静态初始化并手动设置它?
- 编辑
当我试图执行此代码时
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Run Code Online (Sandbox Code Playgroud)
我明白了
错误:(33,49)错误:Builder(Context)在Builder中不公开; 无法从外部包裹访问
realm ×10
android ×5
swift ×3
migration ×2
database ×1
ios ×1
react-native ×1
swift2 ×1
uitableview ×1