Core Data fetch using a predicate
I have two core data entities, one is called Person and the other is Codes. A Person has a one-to-many relationship with Codes.
I want to retrieve all the Codes for a particular Person and have the following function:
static func fetchCodes(person: Person) -> [Codes]? {
let context = getContext()
let fetchRequest : NSFetchRequest<Codes> = Codes.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: #keyPath(Codes.number), ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
//print("person in fetchCodes=(person.first)")
fetchRequest.predicate = NSPredicate(format: "whosAccount == %@", person)
do {
let codes: [Codes] = try context.fetch(Codes.fetchRequest())
//print("Codes count from fetchCodes: (codes.count)")
return codes
} catch {
return nil
}
}
I have checked that the argument person
is in fact the correct Person that I want to retrieve codes for but the result returns all Codes for all Persons. What am I missing?
swift core-data
add a comment |
I have two core data entities, one is called Person and the other is Codes. A Person has a one-to-many relationship with Codes.
I want to retrieve all the Codes for a particular Person and have the following function:
static func fetchCodes(person: Person) -> [Codes]? {
let context = getContext()
let fetchRequest : NSFetchRequest<Codes> = Codes.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: #keyPath(Codes.number), ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
//print("person in fetchCodes=(person.first)")
fetchRequest.predicate = NSPredicate(format: "whosAccount == %@", person)
do {
let codes: [Codes] = try context.fetch(Codes.fetchRequest())
//print("Codes count from fetchCodes: (codes.count)")
return codes
} catch {
return nil
}
}
I have checked that the argument person
is in fact the correct Person that I want to retrieve codes for but the result returns all Codes for all Persons. What am I missing?
swift core-data
What you are missing is a typo:context.fetch(Codes.fetchRequest())
should becontext.fetch(fetchRequest)
.
– pbasdf
Nov 20 '18 at 22:22
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21
add a comment |
I have two core data entities, one is called Person and the other is Codes. A Person has a one-to-many relationship with Codes.
I want to retrieve all the Codes for a particular Person and have the following function:
static func fetchCodes(person: Person) -> [Codes]? {
let context = getContext()
let fetchRequest : NSFetchRequest<Codes> = Codes.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: #keyPath(Codes.number), ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
//print("person in fetchCodes=(person.first)")
fetchRequest.predicate = NSPredicate(format: "whosAccount == %@", person)
do {
let codes: [Codes] = try context.fetch(Codes.fetchRequest())
//print("Codes count from fetchCodes: (codes.count)")
return codes
} catch {
return nil
}
}
I have checked that the argument person
is in fact the correct Person that I want to retrieve codes for but the result returns all Codes for all Persons. What am I missing?
swift core-data
I have two core data entities, one is called Person and the other is Codes. A Person has a one-to-many relationship with Codes.
I want to retrieve all the Codes for a particular Person and have the following function:
static func fetchCodes(person: Person) -> [Codes]? {
let context = getContext()
let fetchRequest : NSFetchRequest<Codes> = Codes.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: #keyPath(Codes.number), ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
//print("person in fetchCodes=(person.first)")
fetchRequest.predicate = NSPredicate(format: "whosAccount == %@", person)
do {
let codes: [Codes] = try context.fetch(Codes.fetchRequest())
//print("Codes count from fetchCodes: (codes.count)")
return codes
} catch {
return nil
}
}
I have checked that the argument person
is in fact the correct Person that I want to retrieve codes for but the result returns all Codes for all Persons. What am I missing?
swift core-data
swift core-data
edited Nov 23 '18 at 11:06
wvteijlingen
8,25112544
8,25112544
asked Nov 20 '18 at 18:47
pdoakpdoak
339211
339211
What you are missing is a typo:context.fetch(Codes.fetchRequest())
should becontext.fetch(fetchRequest)
.
– pbasdf
Nov 20 '18 at 22:22
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21
add a comment |
What you are missing is a typo:context.fetch(Codes.fetchRequest())
should becontext.fetch(fetchRequest)
.
– pbasdf
Nov 20 '18 at 22:22
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21
What you are missing is a typo:
context.fetch(Codes.fetchRequest())
should be context.fetch(fetchRequest)
.– pbasdf
Nov 20 '18 at 22:22
What you are missing is a typo:
context.fetch(Codes.fetchRequest())
should be context.fetch(fetchRequest)
.– pbasdf
Nov 20 '18 at 22:22
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21
add a comment |
2 Answers
2
active
oldest
votes
You don’t need a fetch or a predicate, you access the relation as an attribute
let codes = person.codes
Thank you. That is a much neater solution. This returns a Set which looks like this:First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.
– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there aresorted
methods you can call that returns a sorted array.
– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned byperson.codes
to retrieve thecode = RY6
if I filter onnumber = 1
– pdoak
Nov 21 '18 at 13:55
add a comment |
Since you have the Person
you don't need to fetch any data, just get the codes
from the relationship
static func fetchCodes(person: Person) -> [Codes] {
return person.codes
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399579%2fcore-data-fetch-using-a-predicate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don’t need a fetch or a predicate, you access the relation as an attribute
let codes = person.codes
Thank you. That is a much neater solution. This returns a Set which looks like this:First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.
– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there aresorted
methods you can call that returns a sorted array.
– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned byperson.codes
to retrieve thecode = RY6
if I filter onnumber = 1
– pdoak
Nov 21 '18 at 13:55
add a comment |
You don’t need a fetch or a predicate, you access the relation as an attribute
let codes = person.codes
Thank you. That is a much neater solution. This returns a Set which looks like this:First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.
– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there aresorted
methods you can call that returns a sorted array.
– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned byperson.codes
to retrieve thecode = RY6
if I filter onnumber = 1
– pdoak
Nov 21 '18 at 13:55
add a comment |
You don’t need a fetch or a predicate, you access the relation as an attribute
let codes = person.codes
You don’t need a fetch or a predicate, you access the relation as an attribute
let codes = person.codes
answered Nov 20 '18 at 18:53
Joakim DanielsonJoakim Danielson
8,0913724
8,0913724
Thank you. That is a much neater solution. This returns a Set which looks like this:First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.
– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there aresorted
methods you can call that returns a sorted array.
– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned byperson.codes
to retrieve thecode = RY6
if I filter onnumber = 1
– pdoak
Nov 21 '18 at 13:55
add a comment |
Thank you. That is a much neater solution. This returns a Set which looks like this:First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.
– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there aresorted
methods you can call that returns a sorted array.
– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned byperson.codes
to retrieve thecode = RY6
if I filter onnumber = 1
– pdoak
Nov 21 '18 at 13:55
Thank you. That is a much neater solution. This returns a Set which looks like this:
First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.– pdoak
Nov 21 '18 at 9:40
Thank you. That is a much neater solution. This returns a Set which looks like this:
First code is: Optional(<Codes: 0x600002136440> (entity: Codes; id: 0x61480dce0533d92b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p225> ; data: { code = RY6; number = 1; whosAccount = "0x61480dce3c73d90b <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p4>"; }))
. How do I just extract the code and the number element? Also, I will need to sort the Set based on the number element.– pdoak
Nov 21 '18 at 9:40
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there are
sorted
methods you can call that returns a sorted array.– Joakim Danielson
Nov 21 '18 at 11:07
I think you should read the documentation for Set but you can iterate over a set to access all the Codes objects. Set is unordered but there are
sorted
methods you can call that returns a sorted array.– Joakim Danielson
Nov 21 '18 at 11:07
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned by
person.codes
to retrieve the code = RY6
if I filter on number = 1
– pdoak
Nov 21 '18 at 13:55
I am fairly new to Xcode/swift and have read the Set documentation but don't understand how to filter the set returned by
person.codes
to retrieve the code = RY6
if I filter on number = 1
– pdoak
Nov 21 '18 at 13:55
add a comment |
Since you have the Person
you don't need to fetch any data, just get the codes
from the relationship
static func fetchCodes(person: Person) -> [Codes] {
return person.codes
}
add a comment |
Since you have the Person
you don't need to fetch any data, just get the codes
from the relationship
static func fetchCodes(person: Person) -> [Codes] {
return person.codes
}
add a comment |
Since you have the Person
you don't need to fetch any data, just get the codes
from the relationship
static func fetchCodes(person: Person) -> [Codes] {
return person.codes
}
Since you have the Person
you don't need to fetch any data, just get the codes
from the relationship
static func fetchCodes(person: Person) -> [Codes] {
return person.codes
}
answered Nov 20 '18 at 18:53
vadianvadian
146k13157175
146k13157175
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399579%2fcore-data-fetch-using-a-predicate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you are missing is a typo:
context.fetch(Codes.fetchRequest())
should becontext.fetch(fetchRequest)
.– pbasdf
Nov 20 '18 at 22:22
@pbasdf:Thank you. That was the problem
– pdoak
Nov 21 '18 at 9:21