úterý 30. září 2014

Core data - metadataForPersistentStoreOfType fails after iPhone restart.

In our application, we often need to do core data migration when we create a new release. To do this we first need to get the metadata from our app’s persistent store,  so that it can be migrated. We use the following line of code for this:

[NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:self.storeURL error:&error];

However, we noticed that after restarting the iPhone, our application wasn’t able to run. We discovered this error in the log:

Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x146570e0 {NSUnderlyingException=error during prepareSQL for SQL string 'SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'' : attempt to write a readonly database, NSSQLiteErrorDomain=264}


We determined that this is a core data bug, if you do not need to call metadataForPersistentStoreOfType you won’t see that there is a problem.  We noticed that this bug occurs only if we use WAL journal mode in SQLite, when we changed it to DELETE journal mode the app worked as expected.

pondělí 15. září 2014

Using SQLite database in Portable Class Library

I am a member of the team who are developing the C# .NET version of the secure messaging application called Babel.

One of the components which we need is database.  We chose SQLite for this component because it is optimal for our project and recommended for the Portable class library (PCL). We also want to use a framework for Object-Relation Mapping (ORM). After trying several options we decided to use SQLite-NET and its extension for object relationships SQLite-NET Extensions . This setup initially looked good but after some testing we discovered that the SQLite components target .NET 4.0, Win 8, WinPhone 8.1, Silverlight 5 and WinPhoneSilverlight 8.1. However our PCL needs to target .NET 4.5, Win 8.1 and WinPhone 8.1. This is a big problem because we are unable to use the SQLite component in the current build of our PCL.

We download both projects from GIT and delete every project which we cannot use (there are projects targeting every platform) then change the targeted platforms for the PCL projects to .NET 4.5, Win 8.1 and WinPhone 8.1. This will generate many errors in the code that we will need to repair before the project will build. But once the project does build the PCL components will all have the correct target platforms that we can use in our project.


If you want this component with the same target platforms here is a zip containing the compiled C# .NET libraries.