领域sumOfProperty不提供最新更新

时间:2022-03-22 19:34:22

Realm 0.96.3 master.

境界0.96.3掌握。

I have a test that creates two records of Person. Each person has an NSNumber<RLMDouble> *walkDistance property.

我有一个测试,创建了两个Person记录。每个人都有一个NSNumber * walkDistance属性。

model of Person

@interface Person : RLMObject
@property NSString  *id;
@property NSString  *name;
@property NSDate    *birthdate;
@property NSNumber<RLMDouble>  *walkDistance;
@property RLMArray<Dog *><Dog> *dogs;
@end
RLM_ARRAY_TYPE(Person)

viewDidLoad

In a tableview, I created a RLMRealm self.database and a RLMResult self.persons. on the main thread in viewDidLoad:

在tableview中,我创建了一个RLMRealm self.database和一个RLMResult self.persons。在viewDidLoad的主线程上:

self.bgq = dispatch_queue_create("com.salesram.bgqueu", NULL);
self.database = [self getEncryptedRealm];
self.persons = [[Person objectsInRealm:self.database withPredicate:nil]
__typeof__(self) __weak weakSelf = self;
self.token = [self.database addNotificationBlock:^(NSString * _Nonnull notification, RLMRealm * _Nonnull realm) {
    [weakSelf.tableView reloadData];
    [weakSelf calculateTotal];
}];

The getEncryptedRealm is a standard Realm creation:

getEncryptedRealm是标准的Realm创建:

    NSData *key = [NSData dataWithBytes:c length:64];
    // Open the encrypted Realm file
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.encryptionKey = key;
    NSError *error;
    RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
    assert(realm != nil);

    return realm;

createData:

I have a function createData to create 2 persons in a background thread. Another function clears all objects in a background thread then add 2 new person. Both of them use GCD same queue. So they runs sequentially. [are on the same thread and (Edit: they can be run in different threads)].

我有一个函数createData在后台线程中创建2个人。另一个函数清除后台线程中的所有对象,然后添加2个新人。它们都使用GCD相同的队列。所以他们按顺序运行。 [在同一个线程上(编辑:它们可以在不同的线程中运行)]。

- (void)createData:(int)seq
{
dispatch_async(self.bgq, ^{
    NSLog(@"start %i", seq);
    @autoreleasepool {
        RLMRealm *db = [self getEncryptedRealm];
        //[db refresh];
        [db beginWriteTransaction];
        for (int i = 0; i < 2; i++) {

            NSString *pid = [NSString stringWithFormat:@"%i", arc4random()%100];
            Person *person = [Person createOrUpdateInRealm:db withValue:@{@"id": [NSString stringWithFormat:@"%@", pid]}];
            person.name = [NSString stringWithFormat:@"Person Name # %@", pid];
            person.walkDistance = [NSNumber numberWithDouble:arc4random()%3+1];
            // omits some code of creating dogs...
        }
        [db commitWriteTransaction];

        NSLog(@"end %i", seq);
    }
});
}

clearDB:

The clearDB will delete everything and recreate everything on a background serial queue.

clearDB将删除所有内容并在后台串行队列上重新创建所有内容。

- (IBAction)clearDB:(UIBarButtonItem *)sender
{
    dispatch_async(self.bgq, ^{
        RLMRealm *db = [self getEncryptedRealm];
        [db beginWriteTransaction];
        [db deleteAllObjects];
        [db commitWriteTransaction];
    });
    [self createData:1];
}

calculateTotal

Then I have a function on the main thread to calculate the sum of walkDistance. It will be triggered by the Realm notification on commitWrite.

然后我在主线程上有一个函数来计算walkDistance的总和。它将由commitWrite上的Realm通知触发。

- (void)calculateTotal
{
    assert([NSThread isMainThread]);
    NSNumber *total = [self.persons sumOfProperty:@"walkDistance"];
    double miles = 0;
    for (Person *person in self.persons) {
        miles += [person.walkDistance doubleValue];
    }
    NSString *title = [NSString stringWithFormat:@"%@ - %0.0f", total, miles];
    NSLog(@"%@", title);
}

Problem

The problem is when I trigger clearDB with a button, most of the time I got a total of distance as 0 with the sumOfProperty method. The for loop add always work.

问题是当我用按钮触发clearDB时,大部分时间我使用sumOfProperty方法得到总距离为0。 for循环添加始终有效。

From the log, it seems two notifications was fired and the calculateTotal is called two times, one after the clear, one after the population. But the sumOfProperty seems most of the time stick with the first time value, which is 0. You can see the last line of log shows 0 - 6 Instead it should be 6 - 6

从日志中,似乎有两个通知被触发,calculateTotal被调用两次,一个在clear之后,一个在人口之后。但是sumOfProperty似乎大部分时间都坚持使用第一个时间值,即0。你可以看到最后一行的日志显示0 - 6而不是它应该是6 - 6

NSLog output

2015-12-10 21:25:38.686 dbrealmtest[18746:3258269] start 1
2015-12-10 21:25:38.686 dbrealmtest[18746:3258213] 1.358118751448067e-312 - 0
2015-12-10 21:25:38.692 dbrealmtest[18746:3258269] end 1
2015-12-10 21:25:38.693 dbrealmtest[18746:3258213] 0 - 6

Is this a known issue? a bug? or anything I missed?

这是一个已知的问题?一个bug?或者我错过了什么?

Thanks

1 个解决方案

#1


0  

Update for the records: This turned out after further investigation to be a bug, which is fixed since version 0.97.1.

更新记录:经过进一步调查后发现这是一个错误,自版本0.97.1以来已修复。

#1


0  

Update for the records: This turned out after further investigation to be a bug, which is fixed since version 0.97.1.

更新记录:经过进一步调查后发现这是一个错误,自版本0.97.1以来已修复。