magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

MagicalRecoard + XCTest = Crash ? #1144

Closed kenmaz closed 8 years ago

kenmaz commented 8 years ago

I use MR2.3.0. When I run Unit Test, it crashes.

I have a very simple View Controller.

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [MagicalRecord setupCoreDataStack];

    //***** Run Unit Test (cmd+U) and Crash ******
    //***** If comment out the following code, Unit Test is passed without crash.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{   //(1)
        NSLog(@"do somethig");  //(2)
        dispatch_async(dispatch_get_main_queue(), ^{
            Magazine* magazine = [Magazine MR_createEntity]; //(3)
            NSLog(@"%@", magazine);
        });
    });
    /////////////////////////////////////////////////
}

and Test Code.

@implementation MRCrashTests

- (void)setUp {
    [super setUp];
}

- (void)tearDown {
    [super tearDown];
}

- (void)testExample {
    // do nothing.
}

This App run very well in iOS Simulator and actual device. But run Unit Test (cmd + U) , it crashes with error trace.

2015-12-02 22:02:04.187 MRCrash[54170:1548606] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Default context is nil! Did you forget to initialize the Core Data Stack?'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001035a5f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010301fdeb objc_exception_throw + 48
    2   CoreFoundation                      0x00000001035a5daa +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x0000000102c6c5ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   MRCrashTests                        0x000000010fbf9665 +[NSManagedObjectContext(MagicalRecord) MR_defaultContext] + 293
    5   MRCrashTests                        0x000000010fbfdab1 +[NSManagedObjectContext(MagicalThreading) MR_contextForCurrentThread] + 81
    6   MRCrashTests                        0x000000010fbf6279 +[NSManagedObject(MagicalRecord) MR_createEntity] + 57
    7   MRCrash                             0x0000000102768293 __29-[ViewController viewDidLoad]_block_invoke_2 + 35
    8   libdispatch.dylib                   0x0000000105da4d59 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x0000000105dc04a7 _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x0000000105daaf2d _dispatch_main_queue_callback_4CF + 714
    11  CoreFoundation                      0x00000001035062e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    12  CoreFoundation                      0x00000001034c78a9 __CFRunLoopRun + 2073
    13  CoreFoundation                      0x00000001034c6e08 CFRunLoopRunSpecific + 488
    14  Foundation                          0x0000000102c0c3a1 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 267
    15  XCTest                              0x000000010cc4a76a -[XCTestDriver _checkForTestManager] + 234
    16  XCTest                              0x000000010cc92b23 _XCTestMain + 628
    17  CoreFoundation                      0x00000001034d1ffc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    18  CoreFoundation                      0x00000001034c7c85 __CFRunLoopDoBlocks + 341
    19  CoreFoundation                      0x00000001034c73e2 __CFRunLoopRun + 850
    20  CoreFoundation                      0x00000001034c6e08 CFRunLoopRunSpecific + 488
    21  GraphicsServices                    0x0000000106d54ad2 GSEventRunModal + 161
    22  UIKit                               0x000000010395230d UIApplicationMain + 171
    23  MRCrash                             0x000000010276846f main + 111
    24  libdyld.dylib                       0x0000000105def92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

*** Test operation was canceled. If you believe this error represents a bug, please attach the log file at /var/folders/lk/k3hh63dd7k1gzdvwj7c71j3mf_8f20/T/com.apple.dt.XCTest-status/Session-2015-12-02_22:02:00-0Oc3Oj.log

For investigation, I set break-points at (1),(2),(3) line as above code and print MR's defaultContext in lldb.

At line (1)

(lldb) po [NSManagedObjectContext MR_defaultContext]
<NSManagedObjectContext: 0x7fe19c104a70>

The defaultContext is live.

At line (2)

(lldb) po [NSManagedObjectContext MR_defaultContext]
objc[57498]: Class PodsDummy_MagicalRecord is implemented in both /Users/kentaro.matsumae/Library/Developer/CoreSimulator/Devices/3DFFA2F1-86BC-4265-85AD-C524C1BB10FD/data/Containers/Bundle/Application/A60FA8F9-938A-4ED3-BC1B-6520F2FE64D8/MRCrash.app/MRCrash and /Users/kentaro.matsumae/Library/Developer/Xcode/DerivedData/MRCrash-dbggjizxyejxhthexerxbdhdirxc/Build/Products/Debug-iphonesimulator/MRCrashTests.xctest/MRCrashTests. One of the two will be used. Which one is undefined.
objc[57498]: Class MagicalRecord is implemented in both /Users/kentaro.matsumae/Library/Developer/CoreSimulator/Devices/3DFFA2F1-86BC-4265-85AD-C524C1BB10FD/data/Containers/Bundle/Application/A60FA8F9-938A-4ED3-BC1B-6520F2FE64D8/MRCrash.app/MRCrash and /Users/kentaro.matsumae/Library/Developer/Xcode/DerivedData/MRCrash-dbggjizxyejxhthexerxbdhdirxc/Build/Products/Debug-iphonesimulator/MRCrashTests.xctest/MRCrashTests. One of the two will be used. Which one is undefined.

<NSManagedObjectContext: 0x7fe19c104a70>

The defaultContext is live. But strange message is shown.

At line (3)

(lldb) po [NSManagedObjectContext MR_defaultContext]
2015-12-02 22:33:54.724 MRCrash[57498:1661271] *** Assertion failure in +[NSManagedObjectContext MR_defaultContext], /Users/kentaro.matsumae/Desktop/MRCrash/Pods/MagicalRecord/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m:47
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3)..
The process has been returned to the state before expression evaluation.

The defaultContext is dead. Assertion failure is from..

+ (NSManagedObjectContext *) MR_defaultContext
{
    @synchronized(self) {
        NSAssert(MagicalRecordDefaultContext != nil, @"Default context is nil! Did you forget to initialize the Core Data Stack?");
        return MagicalRecordDefaultContext;
    }
}

I don't know why this crash happen. But according to above message at (2), MR instance may be duplicated. Something cocoapod setting is wrong ?

Could you help me.

Environment: Xcode 7.1.1 (7B1005) OSX 10.10.5 Simulator: iPhone5S (iOS9.1) Magical Recoard 2.3.0 installed by CocoaPods

Sample Code: https://github.com/kenmaz/MagicalRecoard-Crash

kenmaz commented 8 years ago

Same problem http://stackoverflow.com/questions/32952060/xcode-7-magical-record-unit-tests-fail

kenmaz commented 8 years ago

Sorry, this is my mistake. Fix Podfile and resolve this problem.

https://github.com/kenmaz/MagicalRecoard-Crash/commit/e5a452300e9056fe2dc7f5ae00634cb1e5168df8#diff-4a25b996826623c4a3a4910f47f10c30

tonyarnold commented 8 years ago

Good to hear you got it sorted, @kenmaz!