realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.21k stars 2.14k forks source link

Weird inserting crash that occurs at random occasions #4919

Closed mustafa96m closed 7 years ago

mustafa96m commented 7 years ago

Hi guys,

I'm using realm as a caching db to cache responses and requests I created an realm object called Cache managed by singleton object called CacheManager which is used in Networking singleton object which is NetworkManager

Cache.h:


#import <Realm/Realm.h>

@interface Cache : RLMObject <RLMThreadConfined>

@property  NSString *cacheId; 
@property  NSString *cachedRequest;
@property   NSString *cachedResponse;
@property   NSString *cacheAPI;
@property   NSDate *created_at;
@property  NSDate *expires_at;

@end

// This protocol enables typed collections. i.e.:
// RLMArray<Cache>
RLM_ARRAY_TYPE(Cache)

Cache.m:

#import "Cache.h"

@implementation Cache

// Specify default values for properties

+ (NSDictionary *)defaultPropertyValues
{
    return @{@"cacheId":[[NSProcessInfo processInfo] globallyUniqueString],
             @"created_at":[NSDate date]};
}

+(NSString *)primaryKey{
    return @"cacheId";
}

// Specify properties to ignore (Realm won't persist these)

//+ (NSArray *)ignoredProperties
//{
//    return @[];
//}

@end

Stacktrace

* thread #1: tid = 0x21a0b5, 0x0000000184793014 libsystem_kernel.dylib`__pthread_kill + 8, stop reason = signal SIGABRT
    frame #0: 0x0000000184793014 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x000000018485b450 libsystem_pthread.dylib`pthread_kill + 112
    frame #2: 0x0000000184707400 libsystem_c.dylib`abort + 140
    frame #3: 0x00000001841d12d4 libc++abi.dylib`abort_message + 132
    frame #4: 0x00000001841eecc0 libc++abi.dylib`default_terminate_handler() + 304
    frame #5: 0x00000001841fc844 libobjc.A.dylib`_objc_terminate() + 124
    frame #6: 0x00000001841eb66c libc++abi.dylib`std::__terminate(void (*)()) + 16
    frame #7: 0x00000001841eb234 libc++abi.dylib`__cxa_rethrow + 144
  * frame #8: 0x000000010032b370 PROJECT`RLMRealmTranslateException(error=<unavailable>) + 24 at RLMRealm.mm:269
    frame #9: 0x000000010032e8fc PROJECT`::-[RLMRealm commitWriteTransaction:](self=<unavailable>, _cmd=<unavailable>, outError=<unavailable>) + 108 at RLMRealm.mm:550
    frame #10: 0x000000010032ede0 PROJECT`::-[RLMRealm transactionWithBlock:error:](self=<unavailable>, _cmd=<unavailable>, block=<unavailable>, outError=<unavailable>)(), NSError **) + 152 at RLMRealm.mm:581
    frame #11: 0x000000010032ecf8 PROJECT`::-[RLMRealm transactionWithBlock:](self=<unavailable>, _cmd=<unavailable>, block=<unavailable>)()) + 72 at RLMRealm.mm:574
    frame #12: 0x000000010009fdcc PROJECT`-[CacheManager cacheResponse:Path:AndParameters:](self=<unavailable>, _cmd=<unavailable>, response=<unavailable>, path=<unavailable>, params=<unavailable>) + 1436 at CacheManager.m:129
    frame #13: 0x00000001000ad5f4 PROJECT`__107-[SessionManager requestAPI:WithParameters:WithSuccessResponseCallback:failResponseCallback:errorCallback:]_block_invoke.76((null)=<unavailable>, response=<unavailable>) + 140 at SessionManager.m:75
    frame #14: 0x0000000100087b00 PROJECT`__114-[NetworkManager requestAPIWithPath:AndParameters:WithSuccessResponseCallback:failResponseCallback:errorCallback:]_block_invoke((null)=<unavailable>, task=<unavailable>, responseObject=<unavailable>) + 288 at NetworkManager.m:62
    frame #15: 0x00000001000b8aa8 PROJECT`__116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke.97((null)=<unavailable>, response=<unavailable>, responseObject=<unavailable>, error=<unavailable>) + 212 at AFHTTPSessionManager.m:287
    frame #16: 0x00000001000dab84 PROJECT`__72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2.150((null)=<unavailable>) + 224 at AFURLSessionManager.m:308
    frame #17: 0x0000000100ce9258 libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #18: 0x0000000100ce9218 libdispatch.dylib`_dispatch_client_callout + 16
    frame #19: 0x0000000100cedfac libdispatch.dylib`_dispatch_main_queue_callback_4CF + 476
    frame #20: 0x0000000185772810 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
    frame #21: 0x00000001857703fc CoreFoundation`__CFRunLoopRun + 1660
    frame #22: 0x000000018569e2b8 CoreFoundation`CFRunLoopRunSpecific + 444
    frame #23: 0x0000000187152198 GraphicsServices`GSEventRunModal + 180
    frame #24: 0x000000018b6e57fc UIKit`-[UIApplication _run] + 684
    frame #25: 0x000000018b6e0534 UIKit`UIApplicationMain + 208
    frame #26: 0x00000001000893b8 PROJECT`main(argc=<unavailable>, argv=<unavailable>) + 124 at main.m:14
    frame #27: 0x00000001846815b8 libdyld.dylib`start + 4
(lldb) 

Console Output

2017-05-02 21:59:59.059237 PROJECT[7631:2203829] Adding New ones .. 
2017-05-02 21:59:59.081163 PROJECT[7631:2203829] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1857c51b8 0x1841fc55c 0x1857c5100 0x18624678c 0x1000a0600 0x1000b2aa0 0x10032e070 0x10033ed88 0x1001b3520 0x1003823c4 0x10032e8c4 0x10032ede0 0x10032ecf8 0x10009fdcc 0x1000ad5f4 0x100087b00 0x1000b8aa8 0x1000dab84 0x100ce9258 0x100ce9218 0x100cedfac 0x185772810 0x1857703fc 0x18569e2b8 0x187152198 0x18b6e57fc 0x18b6e0534 0x1000893b8 0x1846815b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

Steps to Reproduce

requestString : id=2132132&color=3213213&block=Stringstringstring responseString : [Big json object as a string] [ 7,344 bytes ] cacheAPI : page.php

try to insert from singleton object.

Code Sample

        NSLog(@"Adding New ones .. ");

        Cache *mycache = [Cache new];
        mycache.cacheAPI = path;
        mycache.cachedRequest = requestString;
        mycache.cachedResponse = responseString;

        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm transactionWithBlock:^{
            [realm addObject:mycache];
        }];

        NSLog(@"I should've created something new ... %@",mycache);

Version of Realm and Tooling

Realm framework version:

Xcode version: 8.2.1

iOS/OSX version: 10.2.1

Dependency manager + version: Cocoapods

jpsim commented 7 years ago

Looks like code that you're running in a Realm write transaction is throwing that exception. Realm doesn't handle exceptions thrown by your code, you have to handle those yourself.

Please keep in mind that most Objective-C frameworks don't throw exceptions as control flow or runtime handlable error reporting, but rather to indicate programmer error or API misuse, so you should instead avoid the exception being thrown rather than catching it explicitly.

mustafa96m commented 7 years ago

I debugged it after crash this what it's showing,

http://imgur.com/kEklywN

Looks fine. requestString, responseString can't be nil because I'm checking them first then proceed with inserting them to Realm.

Thanks for the reply.

jpsim commented 7 years ago

Is there anything more we can help you with?

mustafa96m commented 7 years ago

Thanks so much, though I'm a little bit confused correct me if I'm mistaken the exception is being thrown by AFNetworking @ this stack and got handled by realm right?

16  ****                              0x000a9699 __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2.150 + 217
jpsim commented 7 years ago

What makes you think that this is an exception being thrown, or that Realm is handling it?

mustafa96m commented 7 years ago

Because of transactionWithBlock: has try-except right? this why it's catching the exception "Data parameter is nil"

Yet I couldn't identify why it's happening because it's really odd. I've tested on two devices requesting the same API same source yet one goes well the other crashes.

jpsim commented 7 years ago

Because of transactionWithBlock: has try-except right?

No, we only catch exceptions thrown by Realm, not by anything in your code or the frameworks you call into.

mustafa96m commented 7 years ago

So any idea where "Data parameter is nil" is coming from?

jpsim commented 7 years ago

That's not being thrown by anything inside Realm.

mustafa96m commented 7 years ago

Alright, Thanks.

austinzheng commented 7 years ago

Please open a new issue if you have any other problems. Thanks!