无法理解Realm中的allObjects()

时间:2016-08-12 16:49:38

标签: ios swift realm

我正在制作一个使用Realm作为离线数据库的应用。问题是,当我打电话给#include <QApplication> #include <QtGui/QPainter> #include <QtGui/QPainterPath> #include <QtGui/QFont> #include <QtPrintSupport/QPrintDialog> #include <QtPrintSupport/QPrinter> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPrinter *printer = new QPrinter(); QPrintDialog dialog(printer); if (dialog.exec() == QDialog::Accepted) { QFont font("Arial"); font.setPointSize(48); QPainter painter(printer); painter.setFont(font); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(QBrush(QColor(Qt::black))); painter.setPen(Qt::NoPen); // drawPath() QPainterPath path_drawPath; path_drawPath.addText(100, 200, font, "मेदितेरेनियन सि"); painter.drawPath(path_drawPath); // drawPolygon() QPainterPath path_drawPoly; path_drawPoly.addText(100, 300, font, "मेदितेरेनियन सि"); QPolygonF poly = path_drawPoly.toFillPolygon(); painter.drawPolygon(poly, Qt::WindingFill); } return 0; } 时,什么都没有显示出来。我做了一些调试,发现了下面的结果

相关代码

Workout.allObjects()

class Workout: RLMObject { dynamic var name: String? dynamic var exercises: RLMArray = RLMArray(objectClassName: "Exercise") dynamic var numberOfWorkouts = 0 convenience init(name: String) { self.init() self.name = name } } func createWorkoutObject() { let realm = RLMRealm.defaultRealm() try! realm.transactionWithBlock { workout = Workout() } } 结束时(带断点),我跑了 enter image description here

所以显然有一个Workout对象,但createWorkoutObject()似乎没有看到它。

这是为什么?我很难理解我做错了什么。

1 个答案:

答案 0 :(得分:1)

allObjects()方法仅返回由给定Realm管理的对象。在createWorkoutObject()中,您创建了Workout的实例,但不要将其添加到Realm中。由于这个新对象不是由Realm管理的,allObjects()不会返回它。

尝试:

func createWorkoutObject() {
    let realm = RLMRealm.defaultRealm()
    try! realm.transactionWithBlock {
        workout = Workout()
        realm.addObject(workout)
    }
}