<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MRo Blog &#187; CoreData</title>
	<atom:link href="http://blog.mro.name/tag/coredata/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mro.name</link>
	<description>Marcus Rohrmoser mobile Software</description>
	<lastBuildDate>Wed, 23 Jun 2010 11:32:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CoreData generic findManyByKey</title>
		<link>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/</link>
		<comments>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 03:32:34 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSEntityDescription]]></category>
		<category><![CDATA[NSManagedObject]]></category>
		<category><![CDATA[NSManagedObjectContext]]></category>
		<category><![CDATA[NSPredicate]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1583</guid>
		<description><![CDATA[The base for many of my SELECT-ish queries when querying by exact match is one generic method I created in some category methods on NSManagedObjectContext: -&#40;NSArray*&#41;entityName:&#40;NSString*&#41;entityName findManyByRelation:&#40;NSDictionary*&#41;dict &#123; // TODO handle dict nil and emptyness NSMutableArray *arr = &#91;&#91;NSMutableArray alloc&#93; initWithCapacity:dict.count&#93;; for&#40;NSString *key in dict&#41; &#123; NSExpression *left = &#91;NSExpression expressionForKeyPath:key&#93;; NSExpression *right = &#91;NSExpression [...]]]></description>
			<content:encoded><![CDATA[<p>The base for many of my <a href="http://en.wikipedia.org/wiki/Select_%28SQL%29">SELECT</a>-ish queries when querying by exact match is one generic method I created in some <a href="http://developer.apple.com/iphone/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1">category methods</a> on <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html">NSManagedObjectContext</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName findManyByRelation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dict
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//  TODO handle dict nil and emptyness</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span>dict.count<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dict<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSExpression</span> <span style="color: #002200;">*</span>left <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForKeyPath<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSExpression</span> <span style="color: #002200;">*</span>right <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForConstantValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSComparisonPredicate</span> <span style="color: #002200;">*</span>cp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSComparisonPredicate</span> alloc<span style="color: #002200;">&#93;</span> initWithLeftExpression<span style="color: #002200;">:</span>left
            rightExpression<span style="color: #002200;">:</span>right modifier<span style="color: #002200;">:</span>NSDirectPredicateModifier type<span style="color: #002200;">:</span>NSEqualToPredicateOperatorType options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>arr addObject<span style="color: #002200;">:</span>cp<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>cp release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #400080;">NSPredicate</span> <span style="color: #002200;">*</span>pred <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>arr.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
        pred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>arr objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;    <span style="color: #11740a; font-style: italic;">// why do I have to retain here?</span>
    <span style="color: #a61390;">else</span>
        pred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCompoundPredicate</span> alloc<span style="color: #002200;">&#93;</span> initWithType<span style="color: #002200;">:</span>NSAndPredicateType subpredicates<span style="color: #002200;">:</span>arr<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSFetchRequest</span> <span style="color: #002200;">*</span>fr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFetchRequest</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    fr.entity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span>entityName inManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
    fr.predicate <span style="color: #002200;">=</span> pred;
    <span style="color: #002200;">&#91;</span>arr release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>pred release<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">//	NSLog(@&quot;predicate effective: %@&quot;, fr.predicate);</span>
&nbsp;
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>err <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>ps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self executeFetchRequest<span style="color: #002200;">:</span>fr error<span style="color: #002200;">:&amp;</span>amp;err<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>err <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> <span style="color: #a61390;">raise</span><span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DB Error&quot;</span> format<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetch problem %@&quot;</span>, fr.predicate.predicateFormat<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>fr release<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> ps;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For example it serves under the hood of it&#8217;s sibling helpers</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName findByPrimaryKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dict
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>ps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self entityName<span style="color: #002200;">:</span>entityName findManyByRelation<span style="color: #002200;">:</span>dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ps <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || ps.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>ps.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>ps objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> <span style="color: #a61390;">raise</span><span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DB Error&quot;</span> format<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Multiple hits for %@&quot;</span>, dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">/</span><span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName selectOrInsertWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dict
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span>o <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self entityName<span style="color: #002200;">:</span>entityName findByPrimaryKey<span style="color: #002200;">:</span>dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>o <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSEntityDescription</span> <span style="color: #002200;">*</span>ed <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span>entityName inManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        o <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSManagedObject</span> alloc<span style="color: #002200;">&#93;</span> initWithEntity<span style="color: #002200;">:</span>ed insertIntoManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dict<span style="color: #002200;">&#41;</span>
            <span style="color: #002200;">&#91;</span>o setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>o autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> o;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Those methods again are called by custom convenience wrappers in the <a href="http://developer.apple.com/iPhone/library/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html">model classes</a> loosely following the <a href="http://martinfowler.com/eaaCatalog/activeRecord.html">ActiveRecord Pattern</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CocoaTouch, CoreData and binary String Search</title>
		<link>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/</link>
		<comments>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 18:27:59 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Binary]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSPredicate]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1321</guid>
		<description><![CDATA[The query optimiser for NSPredicate queries ontop CoreData/SQLite on the iPhone is a bit rudimentary (cough) and so I had to optimise myself to get binary-search enabled quick results: +&#40;NSPredicate*&#41;findBySearchTerm:&#40;NSString*&#41;rawTerm within:&#40;BOOL&#41;within context:&#40;NSManagedObjectContext*&#41;context &#123; NSSet *tokens = &#91;MovieM indexTokens:rawTerm&#93;; if&#40;tokens == nil &#124;&#124; tokens.count &#38;lt; = 0&#41; return &#91;NSPredicate predicateWithFormat:@&#34;FALSEPREDICATE&#34;&#93;; NSMutableArray *preds = &#91;NSMutableArray arrayWithCapacity:tokens.count&#93;; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Query_optimizer">query optimiser</a> for <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html">NSPredicate</a> queries ontop CoreData/SQLite on the iPhone is a bit rudimentary (cough) and so I had to optimise myself to get binary-search enabled quick results:</p>
<p><span id="more-1321"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPredicate</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>findBySearchTerm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rawTerm within<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>within context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectContext</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span>tokens <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>MovieM indexTokens<span style="color: #002200;">:</span>rawTerm<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>tokens <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || tokens.count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FALSEPREDICATE&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>preds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span>tokens.count<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>within <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; context <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// As queries aren't optimised by default we do it ourselves:</span>
	<span style="color: #11740a; font-style: italic;">// 1st: find matching entries from the IndexKey table - leveraging it's index:</span>
        <span style="color: #400080;">NSFetchRequest</span> <span style="color: #002200;">*</span>fr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFetchRequest</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        fr.entity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IndexKey&quot;</span> inManagedObjectContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSMutableSet</span> <span style="color: #002200;">*</span>result <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #a61390;">in</span> tokens<span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
	    <span style="color: #11740a; font-style: italic;">// BETWEEN uses the table-index while BEGINSWITH does not:</span>
            fr.predicate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;key BETWEEN {%@, %@}&quot;</span>, token, <span style="color: #002200;">&#91;</span>MovieM upperBoundSearchString<span style="color: #002200;">:</span>token<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>keys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>context executeFetchRequest<span style="color: #002200;">:</span>fr error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
                NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Oops: %@&quot;</span>, error<span style="color: #002200;">&#41;</span>;
	    <span style="color: #11740a; font-style: italic;">// turn IndexKey entries to movies (join up):</span>
            <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>movs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>keys valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;movie&quot;</span><span style="color: #002200;">&#93;</span>;
	    <span style="color: #11740a; font-style: italic;">// aggregate the results for each token:</span>
            <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
                result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableSet</span> setWithArray<span style="color: #002200;">:</span>movs<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">else</span>
                <span style="color: #002200;">&#91;</span>result intersectSet<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSet</span> setWithArray<span style="color: #002200;">:</span>movs<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#91;</span>fr release<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SELF IN %@&quot;</span>, result<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSPredicate</span> <span style="color: #002200;">*</span>template <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>within<span style="color: #002200;">&#41;</span>
        template <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ANY index.key CONTAiNS $searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">else</span>
        template <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ANY index.key BEGINSWITH $searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #a61390;">in</span> tokens<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>params <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span>token forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>preds addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>template predicateWithSubstitutionVariables<span style="color: #002200;">:</span>params<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCompoundPredicate</span> andPredicateWithSubpredicates<span style="color: #002200;">:</span>preds<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Helpers herein are</p>
<ul>
<li><code>[MovieM indexTokens:rawTerm]</code> folds diacritics and uppercase and cuts at whitespace or interpunction,</li>
<li><code>[MovieM upperBoundSearchString:token]</code> which was inspired by <a href="http://developer.apple.com/mac/library/samplecode/DerivedProperty/listing8.html">Apple Sample Code &#8220;DerivedProperty&#8221;</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CoreData &#8211; Hello World.</title>
		<link>http://blog.mro.name/2009/06/coredata-hello-world/</link>
		<comments>http://blog.mro.name/2009/06/coredata-hello-world/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 16:40:55 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Artikel auf deutsch]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=988</guid>
		<description><![CDATA[CoreData ist im iPhone OS 3.0 ganz frisch dazugekommen und riecht auch noch ein wenig nach Plastik. Leider ist der Beispielcode auf den Apple auch im iPhone CoreData Tutorial immer wieder verweist (Locations Sample Project) dem Umzug der OS 3.0 beta um Opfer gefallen. Also los geht&#8217;s zum ersten Schritt: 1. Ein leeres Projekt Nachdem [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/GettingStartedWithCoreData/index.html#//apple_ref/doc/uid/TP40005316">CoreData</a> ist im iPhone OS 3.0 ganz frisch dazugekommen und riecht auch noch ein wenig nach Plastik.</p>
<p>Leider ist der Beispielcode auf den Apple auch im <a href="http://developer.apple.com/iphone/library/documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html">iPhone CoreData Tutorial</a> immer wieder verweist (<a href="http://developer.apple.com/iphone/prerelease/library/samplecode/Locations/index.html">Locations Sample Project</a>) dem Umzug der OS 3.0 beta um Opfer gefallen.</p>
<p>Also los geht&#8217;s zum ersten Schritt:</p>
<p><span id="more-988"></span><br />
1. Ein leeres Projekt</p>
<p>Nachdem iPhone SDK 3.0 installiert ist: &#8220;XCode -&gt; File -&gt; New Project&#8221; mit &#8220;Use Core Data for storage&#8221; Häkchen&#8221;:</p>
<div id="attachment_989" class="wp-caption aligncenter" style="width: 410px"> <img class="aligncenter wp-image-989" title="xcode-new-coredata-project" src="http://blog.mro.name/wp-content/2009/06/bild-5.png" alt="Neues Projekt mit CoreData Unterstützung" width="400" /><p class="wp-caption-text">Neues Projekt mit CoreData Unterstützung</p></div>
<p>Der Application Delegate enthält dann gleich eine ganze Menge Code zur Verwaltung der Datenquelle, u.a. mit</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>storeUrl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self applicationDocumentsDirectory<span style="color: #002200;">&#93;</span>
stringByAppendingPathComponent<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CoreDataDemo.sqlite&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>den Namen der sqlite DB Datei, in die gespeichert werden soll.</p>
<p>2. Das Schema</p>
<div id="attachment_993" class="wp-caption aligncenter" style="width: 410px"> <img class="aligncenter wp-image-993" title="new-coredata-schema" src="http://blog.mro.name/wp-content/2009/06/bild-6.png" alt="Ein neues CoreData Schema" width="400" /> <p class="wp-caption-text">Ein neues CoreData Schema</p></div>
<p>und sollte so heißen wie die DB oben.</p>
<p>3. Zugriffe</p>
<p>Siehe <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484">Core Data Programming Guide &#8211; Fetching Managed Objects</a>.</p>
<p>Damit die Beispiel-Zugriffe Sinn ergeben, muß das Schema eine Entity &#8220;Employee&#8221; mit Attributen &#8220;lastName&#8221; (String) und &#8220;salary&#8221; (Numeric) enthalten.</p>
<p>Dann sollte der erste Einstieg eigentlich klappen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/06/coredata-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
