DocBase Searching
| December 26th, 2009One thing remained to make my document database at least somewhat useful. We could find documents by document Id, but nothing else, so we’ll fix that now.It turns out that this is actually pretty easy since we’ll take advantage of NSPredicate. Just one new method for BRDocBase is needed:
-(NSSet*)findDocumentsUsingPredicate:(NSPredicate*)predicate error:(NSError**)error;
NSPredicate does almost everything needed for us with one small exception: it doesn’t really play well a heterogeneous collection of objects. If the predicate refers to a property that doesn’t exist on an object, it throws an exception. Therefore, we have to wrap every evaluation of the predicate in a try/catch block. I’m not really sure about the overhead of a try/catch (it especially worries me since an exceptions might be thrown quite often), but it’s probably not really an optimal solution. Also, we have to evaluate every single document during the search, which certainly won’t scale well. I have a few ideas for optimizing performance but those can wait.
This turned out to be a much shorter solution than I thought it would be, which is great, but it makes for a short post. Oh well, it’s probably just as well. I need to actually start using this in order to see what problems I have with it.
Deploying an SDK
Once last thing I’m going to do is add a new target to deploy this as an SDK. This way, I can build DocBase and easily reference it from any other project (an alternative would be a project reference). To get started, I first renamed the static library from AppLib to BRDocBase. Simply right click the target and select rename.
Next I also renamed the Product Name in build settings.

Now the library will be named libBRDocBase.a when it’s built. Next add a new Copy Files target: right click on Targets and select Add…New Target. The Copy Files target is in the Other category. I named the target DeploySDK, but the name doesn’t matter.

To make sure the library built before it’s copied, it needs to be set as added as a direct dependency. This is done under the General tab in the target info.

I renamed the Copy Files build phase to Copy Lib since we’re going to need some more build phases to copy other files to difference locations (the name isn’t really important, but it just makes things more clear). In the build phase settings, make sure the destination is set to Absolute Path and the full path should be set to: $HOME/Library/SDKs/BRDocBase/$(PLATFORM_NAME).sdk/usr/local/lib

For us, $(PLATFORM_NAME) will expand to macosx. No actual files are being copied yet, however, so drag libBRDocBase.a from the Products group in Xcode into the the Copy Lib build phase.
Header files need to be included with the SDK, so add another Copy Files build phase to the DeploySDK target (I named it Copy Headers). This should be set up identically to the Copy Lib build phase except that the full path should be set to: $HOME/Library/SDKs/BRDocBase/$(PLATFORM_NAME).sdk/usr/local/include/BRDocBase. Now drag DocBase.h into that build phase.
One final piece is needed. Apparently an SDKSettings.plist file is needed for the SDK to be recognized in other projects. I’m not really certain about everything that goes into this file, but I took one from another SDK and hacked it a bit. The settings will be specific for the platform (so different settings will be needed for an iPhone SDK), so we’ll eventually need one for each different platform. I added my hacked one to the project (I put it in a sub folder macosx.sdk in the project folder since I assume I’ll eventually need for SDKSettings.plist files for other platforms). Then yet another Copy Files build phase to and rename it to Copy MacOSX SDK. This time, don’t use the $(PLATFORM_NAME) in the destination since this file is only for MacOS X. Instead use $(HOME)/Library/SDKs/BRDocBase/macosx.sdk. Then drag the settings file we added to the new build phase.
That should do it. When you build that target, it should set up an SDK folder in your Library directory that can be used by other projects.