Realm-cocoa: Crashes inside `+ [RLMObject objectsWithPredicate:]` when selecting with a NSDate

Created on 11 Oct 2016  路  9Comments  路  Source: realm/realm-cocoa

Goals

I'm trying to query and delete objects of a model by a date field, by selecting objects created before a certain date and then deleting them. I'm using NSPredicate to query the objects.

Expected Results

The objects that have the field 'created_at' older than the specified date should be queried normally, and then deleted.

Actual Results

I'm getting hundreds of crashes from my users when selecting the objects with the NSPredicate, inside + [RLMObject objectsWithPredicate:], even before accessing them. I'm only getting the crash reports from Xcode, so I guess there are a lot more that aren't being reported. There's a try/catch block, but Realm does not throw, just crashes.

Here's the stack trace:

0  long long realm::Array::get<4ul>(unsigned long) const + 8
1  realm::Spec::get_public_column_type(unsigned long) const + 48
2  auto (anonymous namespace)::value_of_type_for_query<realm::DateTime>(realm::Query&, (anonymous namespace)::ColumnReference const&) + 120 (table.hpp:1523)
3  void (anonymous namespace)::add_constraint_to_query<(anonymous namespace)::ColumnReference, objc_object* __strong>(realm::Query&, RLMPropertyType, NSPredicateOperatorType, unsigned long, (anonymous namespace)::ColumnReference, objc_object* __strong) + 1404 (RLMQueryUtil.mm:643)
4  (anonymous namespace)::update_query_with_value_expression(RLMSchema*, RLMObjectSchema*, realm::Query&, NSString*, objc_object*, NSComparisonPredicate*) + 1492 (RLMQueryUtil.mm:945)
5  (anonymous namespace)::update_query_with_predicate(NSPredicate*, RLMSchema*, RLMObjectSchema*, realm::Query&) + 3320 (RLMQueryUtil.mm:1151)
6  RLMUpdateQueryWithPredicate(realm::Query*, NSPredicate*, RLMSchema*, RLMObjectSchema*) + 156 (RLMQueryUtil.mm:1218)
7  RLMGetObjects + 256 (RLMObjectStore.mm:471)
8  +[RLMObject objectsWithPredicate:] + 108 (RLMObject.mm:150)
9  -[TGRecipeRealmRepository removeRecipesCreatedAtMoreThan:] + 104 (TGRecipeRealmRepository.m:104)

Steps to Reproduce

I've never been able to reproduce the issue, but it's been happening with users in production for a while now, at least since Realm 0.98.8. I have upgraded to 1.1.0 but the issue keeps happening.

It affects users from all supported iOS version for my app (iOS 8.x, 9.x, 10.0).

Code Sample

Here's my code that calls objectsWithPredicate:

- (void)removeRecipesCreatedAtMoreThan:(NSDate *)date
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"created_at < %@", date];
    RLMResults *recipes = nil;

    @try {
        recipes = [TGRecipe objectsWithPredicate:predicate]; // <---- here's where the crash happens

        if (recipes) {
            RLMRealm *realm = [RLMRealm defaultRealm];

            [realm beginWriteTransaction];
            [realm deleteObjects:recipes];
            [realm commitWriteTransaction];
        }
    } @catch (NSException *exception) {
        // note that there's a catch but Realm doesn't throw, just crashes
    }
}

Version of Realm and Tooling

Realm version: 1.1.0

Xcode version: 8.0

iOS version: 10.0.2 (but has been happening in devices from iOS 8.1 to iOS 10.0)

Dependency manager: Cocoapods 1.1.0.rc.2

Reproduction-Required T-Bug-Crash

All 9 comments

I'm sorry you're affected by this. It's the first I've heard of such an issue. Can you confirm that you still experience it with the latest version of Realm? 2.0.2 as of this writing.

It's unfortunate that you can't reproduce this yourself, and therefore _we_ also don't have a way to reproduce this. It might be difficult for us to make progress on this until we can reproduce this ourselves.

Thanks @jpsim. Yeah, it sucks that I can't reproduce it myself, that's actually why I hadn't posted it before.

I've been updating Realm every time before I release an update to see if this is fixed, but so far it looks like it hasn't. I'll update to the latest version again and see if it that helps.

If it doesn't, though, is there anything I can do to try to reproduce it? Any edge cases? Recommendations for working with Realm and NSDate/NSPredicate? :(

If it doesn't, though, is there anything I can do to try to reproduce it? Any edge cases? Recommendations for working with Realm and NSDate/NSPredicate? :(

I don't have any tips, what you're doing _is_ supported and it's how I'd recommend doing that, but it's clearly not working reliably in your case.

If you do end up experiencing this yourself, sharing the Realm file with us may help identify what's wrong with it.

Have you had the chance to test this with Realm 2.0.2?

@jpsim I've only deployed the update two days ago. Let me check if I already have the crash reports...

@jpsim I've only got very few crash reports. So far, it hasn't been reported. I should probably confirm this in the next days :)

Thanks! We'll keep this open until you can report back in a few days.

The issue remains with 2.0.2 :(
Here are some screenshots of the crash reports (two "different" but not really call stacks).

screen shot 2016-10-22 at 19 15 53
screen shot 2016-10-22 at 19 16 00

I'm also getting many other crashes from Realm that seem to be unrelated. Those existed before but now it looks like there are more.

screen shot 2016-10-22 at 19 19 35

Ok, let's keep this open then, but we won't be able to look into it until you can share either an affected Realm file, or a sample project that can reproduce these crashes.

Was this page helpful?
0 / 5 - 0 ratings