Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated code crashes with Xcode 8.3 #365

Open
regexident opened this issue Apr 3, 2017 · 3 comments
Open

Generated code crashes with Xcode 8.3 #365

regexident opened this issue Apr 3, 2017 · 3 comments

Comments

@regexident
Copy link
Contributor

Reproduce:

  1. Create simple Core Data model with two entities…
╭───────╮       ╭───────╮
│ Alpha │       │ Beta  │
├───────┤       ├───────┤
│ betas │ <-->> │ alpha │
╰───────╯       ╰───────╯

…with betas being optional, while alpha being mandatory.

  1. Set Codegen of entities to Manual/None.

  2. Generate source code via mogenerator:

mogenerator --model ${SRCROOT}/MogeneratorCrash/MogeneratorCrash.xcdatamodeld/MogeneratorCrash.xcdatamodel --human-dir ${SRCROOT}/MogeneratorCrash/Human --machine-dir ${SRCROOT}/MogeneratorCrash/Machine --swift
  1. Add generated code files to project.

  2. Create entities:

guard let appDelegate = NSApplication.shared().delegate as? AppDelegate else {
    fatalError("Could not find AppDelegate")
}

appDelegate.persistentContainer.performBackgroundTask { context in
    let alpha = Alpha(context: context)
    let beta = Beta(context: context)

    alpha.addBetasObject(beta)
}
  1. Run app.

Expected Behavior

Shouldn't crash.

Actual Behavior

Crashes:

2017-04-03 18:13:50.752753 MogeneratorCrash[28856:15552094] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'Alpha' must have a valid NSEntityDescription.'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff9aeefe7b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fffafacfcad objc_exception_throw + 48
	2   CoreData                            0x00007fff9aa828c0 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 528
	3   MogeneratorCrash                    0x0000000100005b8c _TFC16MogeneratorCrash6_AlphacfT6entityCSo19NSEntityDescription10insertIntoGSqCSo22NSManagedObjectContext__S0_ + 76
	4   MogeneratorCrash                    0x00000001000017d7 _TFC16MogeneratorCrash5AlphacfT6entityCSo19NSEntityDescription10insertIntoGSqCSo22NSManagedObjectContext__S0_ + 71
	5   MogeneratorCrash                    0x0000000100001892 _TToFC16MogeneratorCrash5AlphacfT6entityCSo19NSEntityDescription10insertIntoGSqCSo22NSManagedObjectContext__S0_ + 66
	6   CoreData                            0x00007fff9aaf4031 -[NSManagedObject initWithContext:] + 721
	7   MogeneratorCrash                    0x0000000100003334 _TTOFCSo15NSManagedObjectcfT7contextCSo22NSManagedObjectContext_S_ + 36
	8   MogeneratorCrash                    0x0000000100002ca1 _TFCSo15NSManagedObjectCfT7contextCSo22NSManagedObjectContext_S_ + 65
	9   MogeneratorCrash                    0x0000000100002bd3 _TFFC16MogeneratorCrash14ViewController11viewDidLoadFT_T_U_FCSo22NSManagedObjectContextT_ + 51
	10  MogeneratorCrash                    0x0000000100002cec _TTRXFo_oCSo22NSManagedObjectContext__XFdCb_dS___ + 60
	11  CoreData                            0x00007fff9aba9144 __47-[NSPersistentContainer performBackgroundTask:]_block_invoke + 20
	12  CoreData                            0x00007fff9aab7fa5 developerSubmittedBlockToNSManagedObjectContextPerform + 165
	13  libdispatch.dylib                   0x00000001009f0f5c _dispatch_client_callout + 8
	14  libdispatch.dylib                   0x0000000100a08a59 _dispatch_queue_serial_drain + 205
	15  libdispatch.dylib                   0x00000001009fa489 _dispatch_queue_invoke + 1174
	16  libdispatch.dylib                   0x0000000100a01707 _dispatch_queue_override_invoke + 656
	17  libdispatch.dylib                   0x00000001009f31d7 _dispatch_root_queue_drain + 671
	18  libdispatch.dylib                   0x00000001009f2ee8 _dispatch_worker_thread3 + 114
	19  libsystem_pthread.dylib             0x0000000100a6889a _pthread_wqthread + 1299
	20  libsystem_pthread.dylib             0x0000000100a68375 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Further more mogenerator doesn't seem to respect the "Optional" flag or relationships:
It generates @NSManaged open var betas: NSSet which should be @NSManaged open var betas: NSSet?, no?

Additional Information

Xcode 8.3, mogenerator 1.31

A sample project can be found here: https://github.com/regexident/MogeneratorCrash

@regexident
Copy link
Contributor Author

Just checked with v8.2.1. Same crash. Am I doing something wrong?

@ollieatkinson
Copy link

ollieatkinson commented Apr 4, 2017

@regexident seems to be an issue with core data working out the entity name because you have it linked to your project namespace, so you have a couple of different solutions:

  1. remove @objc(Alpha) and @objc(Beta) from the Human. (Stick with project namespace)
  2. remove namespace from your entities, click the entity in the core data model, under "Class" where it is titled "Module" delete the contents which will force it to use the global namespace

@regexident
Copy link
Contributor Author

regexident commented Apr 4, 2017

@ollieatkinson that fixed it, thanks!

Mogenerator should probably handle that automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants