Core Data fetch using a predicate












-1















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.



enter image description hereenter image description here



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?










share|improve this question

























  • 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
















-1















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.



enter image description hereenter image description here



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?










share|improve this question

























  • 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














-1












-1








-1








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.



enter image description hereenter image description here



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?










share|improve this question
















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.



enter image description hereenter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












2 Answers
2






active

oldest

votes


















0














You don’t need a fetch or a predicate, you access the relation as an attribute



let codes = person.codes





share|improve this answer
























  • 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 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



















0














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
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    You don’t need a fetch or a predicate, you access the relation as an attribute



    let codes = person.codes





    share|improve this answer
























    • 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 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
















    0














    You don’t need a fetch or a predicate, you access the relation as an attribute



    let codes = person.codes





    share|improve this answer
























    • 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 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














    0












    0








    0







    You don’t need a fetch or a predicate, you access the relation as an attribute



    let codes = person.codes





    share|improve this answer













    You don’t need a fetch or a predicate, you access the relation as an attribute



    let codes = person.codes






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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



















    • 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 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

















    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













    0














    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
    }





    share|improve this answer




























      0














      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
      }





      share|improve this answer


























        0












        0








        0







        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
        }





        share|improve this answer













        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
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 18:53









        vadianvadian

        146k13157175




        146k13157175






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$