Xcode 4 and Core Data: How to Enable SQL Debugging

Xcode 4 and Core Data: How to enable SQL Debugging

You should be looking at the same place you get NSLOGS

And you should Go to Product -> Edit Scheme -> Then from the left panel select Run YOURAPP.app and go to the main panel's Arguments Tab.

There you can add an Argument Passed On Launch.

You should add -com.apple.CoreData.SQLDebug 4 (number between 1 and 4, higher number makes it more verbose)

Press OK and your are all set.

The key here is to edit the scheme you will be using for testing.

How do I get the CoreData Debug argument to output to the console?

XCode 4 (final) seems to be inconsistent.

Using one argument works on the device, but fails in the simulator:

-com.apple.CoreData.SQLDebug 1

Using two arguments works on the device and in the simulator:

-com.apple.CoreData.SQLDebug

1

How to print Core Data debug values?

Open "Product -> Scheme -> Edit Scheme..." in Xcode and add
to "Arguments Passed on Launch":

-com.apple.CoreData.SQLDebug 3
-com.apple.CoreData.Logging.stderr 1

(The second launch argument is needed for Core Data debugging on
iOS 10/macOS 10.12 or later, see com.apple.CoreData.SQLDebug not working for more information.)

The you'll see all the values that the SQL statements are bound to.
Example output:

test56[1588:c07] CoreData: sql: BEGIN EXCLUSIVE
test56[1588:c07] CoreData: sql: INSERT INTO ZEVENT(Z_PK, Z_ENT, Z_OPT, ZTIMESTAMP) VALUES(?, ?, ?, ?)
test56[1588:c07] CoreData: details: SQLite bind[0] = (int64)13
test56[1588:c07] CoreData: details: SQLite bind[1] = (int64)1
test56[1588:c07] CoreData: details: SQLite bind[2] = (int64)1
test56[1588:c07] CoreData: details: SQLite bind[3] = "368650709.435904"
test56[1588:c07] CoreData: sql: COMMIT

com.apple.CoreData.SQLDebug flag is not displaying SQL

According to this answer, a .storedata file is only generated if you are using NSXMLStoreType. Also NSXMLStoreType seems to be the default. If you are using an NSXMLStoreType, the SQLDebug flag has no effect as this only works for NSSQLiteStoreType.



You can use NSSQLiteStoreType by adding a persistent store of this type instead of the XML store to your NSPersistentStoreCoordinator (if your CoreData code is generated by Xcode, this will be in your AppDelegate):

do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch { ...}

How to log CoreData SQL queries?

Yep, you may do that by putting -com.apple.CoreData.SQLDebug 1 to "Arguments Passed On Launch"

Core Data DB Location Debug v Release Build

What I usually do is have separate debug and release versions of the app. Then I can have both installed on the same device at the same time. The persistent store (and other files) use the same file names, but since they're separate apps the data is separate.

You can do this by duplicating the app target in Xcode, and making a few changes. In the list of targets, right-click your app and select "duplicate". Give the new one a similar but slightly different name. For example, maybe add "-dev" to your app's name to indicate it's the development target.

You'll also need to change the bundle ID and display name of the app. The bundle ID tells iOS it's a different app, and the display name makes it obvious which one you're tapping. You can also add a different app icon if you like.

Now you can build two versions of the app and install both on the same device. The dev version can have whatever code you're working on that's maybe still kind of broken, and the release version can be whatever's ready for release or a version downloaded from the App Store.

com.apple.CoreData.SQLDebug not working

Core Data uses the new unified logging framework starting with iOS 10. There is a known issue in Xcode that interferes with the logging, but you can use -com.apple.CoreData.Logging.stderr 1 to get around it.

EDIT: For clarity, you must specify -com.apple.CoreData.SQLDebug 1 in addition to the above. This actually enables the SQL trace, while the above will let you see it.

From https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html

As part of this transition, Core Data honors user defaults to log to os_log, stderr, or both with ‘com.apple.CoreData.Logging.oslog’ or ‘com.apple.CoreData.Logging.stderr’. Due to a known issue in Xcode, it may be useful to toggle stderr logging on during debugging.

You might also look in the new Console app, which will display logs from devices connected to your Mac.



Related Topics



Leave a reply



Submit