This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fetchRequest = NSFetchRequest<Contact>( | |
entityName: Contact.entity().name! | |
) | |
fetchRequest.predicate = NSCompoundPredicate( | |
orPredicateWithSubpredicates: [ | |
"name", | |
"surname", | |
"city" | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Contact: NSManagedObject { | |
@NSManaged public var name: String | |
@NSManaged public var surname: String | |
@NSManaged public var addresses: Set<Address> | |
} | |
public class Address: NSManagedObject { | |
@NSManaged public var city: String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let contacts: [Contact] = … | |
var results = [Contact]() | |
for contact in contacts { | |
var addressesMatchingQuery = [Address]() | |
for address in contact.addresses { | |
if address.city == searchQuery { | |
addressesMatchingQuery.append(address) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let contacts: [Contact] = … | |
let results = contacts.filter { contact in | |
var addressesMatchingQuery = [Address]() | |
for address in contact.addresses { | |
if address.city == searchQuery { | |
addressesMatchingQuery.append(address) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let contacts: [Contact] = … | |
let results = contacts.filter { contact in | |
let addressesMatchingQuery = contact.addresses.filter { address in | |
address.city == searchQuery | |
} | |
return addressesMatchingQuery.count > 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let contacts: [Contact] = … | |
let results = contacts.filter { contact in | |
contact.addresses.filter { address in | |
address.city == searchQuery | |
}.count > 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"SUBQUERY(addresses, $address, $address.city ==[cd] \"\(searchQuery)\").@count > 0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduce(initialValue) { | |
... | |
if resultIsGoodEnough($0) { | |
break // and return current $0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduce(initialValue) { | |
... | |
if resultIsGoodEnough($0) { | |
return $0 // No break here! | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduce( | |
initialValue, | |
{ ... }, | |
until: resultIsGoodEnough | |
) |
OlderNewer