Searching in Realm with Swift using NSPredicate

1,137 views
Skip to first unread message

Nursultan Bagidolla

unread,
Apr 4, 2015, 12:03:14 AM4/4/15
to realm...@googlegroups.com
Hello! I'm trying to implement a tableView searching for my iOS app. There are very few examples of using search function with this database and I'm kinda lost. There are no official guides for filtering results with Realm and the one I was trying to use is this one:

http://www.raywenderlich.com/81615/introduction-to-realm.

I'm mainly interested in implementing UISearchControllerDelegate and  UISearchResultsUpdatingprotocols and changes to tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) associated with implementing a searchBar. Does anyone has a simple working example of a TableViewController with Search using Realm and written in Swift?

Nursultan Bagidolla

unread,
Apr 4, 2015, 12:14:52 AM4/4/15
to realm...@googlegroups.com
I've implemented 

updateSearchResultsForSearchController()

function, but it calls an exeption: Terminating app due to uncaught exception 'Invalid predicate expressions', reason: 'Predicate expressions must compare a keypath and another keypath or a constant value'

суббота, 4 апреля 2015 г., 10:03:14 UTC+6 пользователь Nursultan Bagidolla написал:

Samuel Giddins

unread,
Apr 6, 2015, 12:27:43 PM4/6/15
to Nursultan Bagidolla, realm...@googlegroups.com
Hi Nursultan,

Can you share the NSPredicate that causes that exception to be thrown?

--
Samuel Giddins



On Sat, Apr 4, 2015 at 4:15 AM UTC, Nursultan Bagidolla <bagi...@gmail.com> wrote:
I've implemented

updateSearchResultsForSearchController()
function, but it calls an exeption: *Terminating app due to uncaught

exception 'Invalid predicate expressions', reason: 'Predicate expressions
must compare a keypath and another keypath or a constant value'*


суббота, 4 апреля 2015 г., 10:03:14 UTC+6 пользователь Nursultan Bagidolla
написал:



On Sat, Apr 4, 2015 at 4:03 AM UTC, Nursultan Bagidolla <bagi...@gmail.com> wrote:
Hello! I'm trying to implement a tableView searching for my iOS app. There are very few examples of using search function with this database and I'm kinda lost. There are no official guides for filtering results with Realm and the one I was trying to use is this one:

http://www.raywenderlich.com/81615/introduction-to-realm.

I'm mainly interested in implementing UISearchControllerDelegate and UISearchResultsUpdatingprotocols and changes to tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) associated with implementing a searchBar. Does anyone has a simple working example of a TableViewController with Search using Realm and written in Swift?

--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/e1f624ce-007f-4000-929d-41b1d0f56b99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:82056079-819#}

Nursultan Bagidolla

unread,
Apr 7, 2015, 3:39:56 AM4/7/15
to realm...@googlegroups.com, bagi...@gmail.com, he...@realm.io
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array
= phrases.objectsWithPredicate(searchPredicate)
searchResults = array as RLMResults


понедельник, 6 апреля 2015 г., 22:27:43 UTC+6 пользователь Samuel Giddins написал:

Samuel Giddins

unread,
Apr 7, 2015, 11:46:31 AM4/7/15
to Nursultan Bagidolla, realm...@googlegroups.com
Nursultan,
The issue there is with the predicate you're constructing. Instead of SELF, you should be using the name of a string property on your object, i.e. NSPredicate(format: "stringProperty CONTAINS[c] %@",
searchController.searchBar.text). Hope that helps!

--
Samuel Giddins
{#HS:82056079-819#}

Samuel Giddins

unread,
Apr 26, 2015, 1:20:51 PM4/26/15
to Nursultan Bagidolla, realm...@googlegroups.com
Nursultan,
I'm just following up to make sure you have everything working?

--
Samuel Giddins



On Tue, Apr 7, 2015 at 3:46 PM UTC, Help <he...@realm.io> wrote:
Nursultan,
The issue there is with the predicate you're constructing. Instead of SELF, you should be using the name of a string property on your object, i.e. NSPredicate(format: "stringProperty CONTAINS[c] %@",
searchController.searchBar.text). Hope that helps!

--
Samuel Giddins



On Tue, Apr 7, 2015 at 7:40 AM UTC, Nursultan Bagidolla <bagi...@gmail.com> wrote:
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@",
searchController.searchBar.text)
let array = phrases.objectsWithPredicate(searchPredicate)
searchResults = array as RLMResults


понедельник, 6 апреля 2015 г., 22:27:43 UTC+6 пользователь Samuel Giddins
написал:



On Mon, Apr 6, 2015 at 4:27 PM UTC, Help <he...@realm.io> wrote:
Hi Nursultan,

Can you share the NSPredicate that causes that exception to be thrown?

--
Samuel Giddins



On Sat, Apr 4, 2015 at 4:15 AM UTC, Nursultan Bagidolla <bagi...@gmail.com> wrote:
I've implemented

updateSearchResultsForSearchController()
function, but it calls an exeption: *Terminating app due to uncaught
exception 'Invalid predicate expressions', reason: 'Predicate expressions
must compare a keypath and another keypath or a constant value'*

суббота, 4 апреля 2015 г., 10:03:14 UTC+6 пользователь Nursultan Bagidolla
написал:



On Sat, Apr 4, 2015 at 4:03 AM UTC, Nursultan Bagidolla <bagi...@gmail.com> wrote:
Hello! I'm trying to implement a tableView searching for my iOS app. There are very few examples of using search function with this database and I'm kinda lost. There are no official guides for filtering results with Realm and the one I was trying to use is this one:

http://www.raywenderlich.com/81615/introduction-to-realm.

I'm mainly interested in implementing UISearchControllerDelegate and UISearchResultsUpdatingprotocols and changes to tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) associated with implementing a searchBar. Does anyone has a simple working example of a TableViewController with Search using Realm and written in Swift?

--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/e1f624ce-007f-4000-929d-41b1d0f56b99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:82056079-819#}
Reply all
Reply to author
Forward
0 new messages