How memory management will work in Model class(Swift or objective C)










-2















I have a model class which has below properties.



@interface CountryModel : NSObject
@property (nonatomic) NSInteger id;
@property (nonatomic, strong) NSString *country;
@property (nonatomic, strong) NSString *dialCode;
@property (nonatomic) BOOL isInEurope;
@end


I'm getting the below data from web service.




"id": 123,
"country_name": "India",
"dialCode": "+91"




Then I'm binding values to the array, Now my question is how memory management will work for the model class? Because there are few has a strong reference. Can anyone explain me.










share|improve this question




























    -2















    I have a model class which has below properties.



    @interface CountryModel : NSObject
    @property (nonatomic) NSInteger id;
    @property (nonatomic, strong) NSString *country;
    @property (nonatomic, strong) NSString *dialCode;
    @property (nonatomic) BOOL isInEurope;
    @end


    I'm getting the below data from web service.




    "id": 123,
    "country_name": "India",
    "dialCode": "+91"




    Then I'm binding values to the array, Now my question is how memory management will work for the model class? Because there are few has a strong reference. Can anyone explain me.










    share|improve this question


























      -2












      -2








      -2


      0






      I have a model class which has below properties.



      @interface CountryModel : NSObject
      @property (nonatomic) NSInteger id;
      @property (nonatomic, strong) NSString *country;
      @property (nonatomic, strong) NSString *dialCode;
      @property (nonatomic) BOOL isInEurope;
      @end


      I'm getting the below data from web service.




      "id": 123,
      "country_name": "India",
      "dialCode": "+91"




      Then I'm binding values to the array, Now my question is how memory management will work for the model class? Because there are few has a strong reference. Can anyone explain me.










      share|improve this question
















      I have a model class which has below properties.



      @interface CountryModel : NSObject
      @property (nonatomic) NSInteger id;
      @property (nonatomic, strong) NSString *country;
      @property (nonatomic, strong) NSString *dialCode;
      @property (nonatomic) BOOL isInEurope;
      @end


      I'm getting the below data from web service.




      "id": 123,
      "country_name": "India",
      "dialCode": "+91"




      Then I'm binding values to the array, Now my question is how memory management will work for the model class? Because there are few has a strong reference. Can anyone explain me.







      ios objective-c automatic-ref-counting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:05







      Sunil aruru

















      asked Nov 13 '18 at 11:51









      Sunil aruruSunil aruru

      257314




      257314






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Simple answer: automatically (provided you haven't disable ARC)



          Slightly longer answer (provided you haven't disabled ARC):



          When you assign values to the properties country and dialCode of an instance of CountryModel then that instance will have strong references to the referenced NSString values.



          If you assign a new value to either of these properties then the previous strong reference will be dropped and replaced by a strong reference to the new value.



          When the instance of CountryModel is no longer strongly reference by anything then the strong references it contains will be dropped.



          When a strong reference is dropped if there are no other strong references to the reference object then that object becomes reclaimable and the memory it occupies returned to the available memory pool.



          Note: if an NSString reference refers to a literal string then the literal string itself is immortal and will never be reclaimed. If you are trying to gian an understanding of when memory is released by monitoring memory usage, tracking dealloc calls, etc. do not use literal strings as your test objects – it is best to use a user-defined type.



          But I've disabled ARC... Then memory will get reclaimed when you manually instruct it to be. If you're getting leaks turning on ARC is your best choice, if you cannot then you've some debugging to do!



          HTH



          Addendum after comment



          Trying to follow reference counts and expecting them to match with actual executing code doesn't work well as there are often references you do not know about in compiled code. For this reason you will see many writers discouraging tracking reference counts.



          Instead think in terms of strong references making an ownership stake in an object, and as long as there is an owner the object survives.



          This might seem to be essentially the same thing, but there is a subtle difference in how the two are viewed: the reference count belongs to the object; while the strong reference attribute, which asserts an ownership stake, is part of the belongs to the referencing variable.



          In your comment example a has an ownership stake in the CountryModel object it references - the only ownership stake you have so far created to that object. The CountryModel object has ownership stakes in the objects that country and dialcode reference, but may not be the only such stakeholders – in that fragment we've no idea what other reference variables assert an ownership stake in the objects those two variables reference.



          Later when you create temparray the object reference stored in a is copied into the array and temparray claims and ownership stake in the referenced CountryModel object – while a continues to assert its own ownership stake as well. So now you have two variables you have created asserting an ownership stake in the CountryModel object.



          A similar explanation applies to b & c.



          When there is no ownership stake claimed by any variable/property over an object then that object can be destroyed. As part of that destruction any variables/properties that are part of that object are destroyed and any ownership stakes they asserted over references stored in them are withdrawn.



          If that process results in more objects having no more ownership stakes asserted over them then those objects too can be destroyed; the process continuing to destroy unwanted objects until the only remaining objects are those where some variable/property asserts an ownership stake over them.



          HTH more than it confuses!






          share|improve this answer

























          • Thanks for answering...from where do you got that countycode instance?

            – Sunil aruru
            Nov 13 '18 at 18:41












          • Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

            – Sunil aruru
            Nov 13 '18 at 18:53












          • @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

            – CRD
            Nov 13 '18 at 20:36











          • @Sunilaruru - see addendum

            – CRD
            Nov 13 '18 at 21:02










          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%2f53280457%2fhow-memory-management-will-work-in-model-classswift-or-objective-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Simple answer: automatically (provided you haven't disable ARC)



          Slightly longer answer (provided you haven't disabled ARC):



          When you assign values to the properties country and dialCode of an instance of CountryModel then that instance will have strong references to the referenced NSString values.



          If you assign a new value to either of these properties then the previous strong reference will be dropped and replaced by a strong reference to the new value.



          When the instance of CountryModel is no longer strongly reference by anything then the strong references it contains will be dropped.



          When a strong reference is dropped if there are no other strong references to the reference object then that object becomes reclaimable and the memory it occupies returned to the available memory pool.



          Note: if an NSString reference refers to a literal string then the literal string itself is immortal and will never be reclaimed. If you are trying to gian an understanding of when memory is released by monitoring memory usage, tracking dealloc calls, etc. do not use literal strings as your test objects – it is best to use a user-defined type.



          But I've disabled ARC... Then memory will get reclaimed when you manually instruct it to be. If you're getting leaks turning on ARC is your best choice, if you cannot then you've some debugging to do!



          HTH



          Addendum after comment



          Trying to follow reference counts and expecting them to match with actual executing code doesn't work well as there are often references you do not know about in compiled code. For this reason you will see many writers discouraging tracking reference counts.



          Instead think in terms of strong references making an ownership stake in an object, and as long as there is an owner the object survives.



          This might seem to be essentially the same thing, but there is a subtle difference in how the two are viewed: the reference count belongs to the object; while the strong reference attribute, which asserts an ownership stake, is part of the belongs to the referencing variable.



          In your comment example a has an ownership stake in the CountryModel object it references - the only ownership stake you have so far created to that object. The CountryModel object has ownership stakes in the objects that country and dialcode reference, but may not be the only such stakeholders – in that fragment we've no idea what other reference variables assert an ownership stake in the objects those two variables reference.



          Later when you create temparray the object reference stored in a is copied into the array and temparray claims and ownership stake in the referenced CountryModel object – while a continues to assert its own ownership stake as well. So now you have two variables you have created asserting an ownership stake in the CountryModel object.



          A similar explanation applies to b & c.



          When there is no ownership stake claimed by any variable/property over an object then that object can be destroyed. As part of that destruction any variables/properties that are part of that object are destroyed and any ownership stakes they asserted over references stored in them are withdrawn.



          If that process results in more objects having no more ownership stakes asserted over them then those objects too can be destroyed; the process continuing to destroy unwanted objects until the only remaining objects are those where some variable/property asserts an ownership stake over them.



          HTH more than it confuses!






          share|improve this answer

























          • Thanks for answering...from where do you got that countycode instance?

            – Sunil aruru
            Nov 13 '18 at 18:41












          • Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

            – Sunil aruru
            Nov 13 '18 at 18:53












          • @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

            – CRD
            Nov 13 '18 at 20:36











          • @Sunilaruru - see addendum

            – CRD
            Nov 13 '18 at 21:02















          0














          Simple answer: automatically (provided you haven't disable ARC)



          Slightly longer answer (provided you haven't disabled ARC):



          When you assign values to the properties country and dialCode of an instance of CountryModel then that instance will have strong references to the referenced NSString values.



          If you assign a new value to either of these properties then the previous strong reference will be dropped and replaced by a strong reference to the new value.



          When the instance of CountryModel is no longer strongly reference by anything then the strong references it contains will be dropped.



          When a strong reference is dropped if there are no other strong references to the reference object then that object becomes reclaimable and the memory it occupies returned to the available memory pool.



          Note: if an NSString reference refers to a literal string then the literal string itself is immortal and will never be reclaimed. If you are trying to gian an understanding of when memory is released by monitoring memory usage, tracking dealloc calls, etc. do not use literal strings as your test objects – it is best to use a user-defined type.



          But I've disabled ARC... Then memory will get reclaimed when you manually instruct it to be. If you're getting leaks turning on ARC is your best choice, if you cannot then you've some debugging to do!



          HTH



          Addendum after comment



          Trying to follow reference counts and expecting them to match with actual executing code doesn't work well as there are often references you do not know about in compiled code. For this reason you will see many writers discouraging tracking reference counts.



          Instead think in terms of strong references making an ownership stake in an object, and as long as there is an owner the object survives.



          This might seem to be essentially the same thing, but there is a subtle difference in how the two are viewed: the reference count belongs to the object; while the strong reference attribute, which asserts an ownership stake, is part of the belongs to the referencing variable.



          In your comment example a has an ownership stake in the CountryModel object it references - the only ownership stake you have so far created to that object. The CountryModel object has ownership stakes in the objects that country and dialcode reference, but may not be the only such stakeholders – in that fragment we've no idea what other reference variables assert an ownership stake in the objects those two variables reference.



          Later when you create temparray the object reference stored in a is copied into the array and temparray claims and ownership stake in the referenced CountryModel object – while a continues to assert its own ownership stake as well. So now you have two variables you have created asserting an ownership stake in the CountryModel object.



          A similar explanation applies to b & c.



          When there is no ownership stake claimed by any variable/property over an object then that object can be destroyed. As part of that destruction any variables/properties that are part of that object are destroyed and any ownership stakes they asserted over references stored in them are withdrawn.



          If that process results in more objects having no more ownership stakes asserted over them then those objects too can be destroyed; the process continuing to destroy unwanted objects until the only remaining objects are those where some variable/property asserts an ownership stake over them.



          HTH more than it confuses!






          share|improve this answer

























          • Thanks for answering...from where do you got that countycode instance?

            – Sunil aruru
            Nov 13 '18 at 18:41












          • Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

            – Sunil aruru
            Nov 13 '18 at 18:53












          • @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

            – CRD
            Nov 13 '18 at 20:36











          • @Sunilaruru - see addendum

            – CRD
            Nov 13 '18 at 21:02













          0












          0








          0







          Simple answer: automatically (provided you haven't disable ARC)



          Slightly longer answer (provided you haven't disabled ARC):



          When you assign values to the properties country and dialCode of an instance of CountryModel then that instance will have strong references to the referenced NSString values.



          If you assign a new value to either of these properties then the previous strong reference will be dropped and replaced by a strong reference to the new value.



          When the instance of CountryModel is no longer strongly reference by anything then the strong references it contains will be dropped.



          When a strong reference is dropped if there are no other strong references to the reference object then that object becomes reclaimable and the memory it occupies returned to the available memory pool.



          Note: if an NSString reference refers to a literal string then the literal string itself is immortal and will never be reclaimed. If you are trying to gian an understanding of when memory is released by monitoring memory usage, tracking dealloc calls, etc. do not use literal strings as your test objects – it is best to use a user-defined type.



          But I've disabled ARC... Then memory will get reclaimed when you manually instruct it to be. If you're getting leaks turning on ARC is your best choice, if you cannot then you've some debugging to do!



          HTH



          Addendum after comment



          Trying to follow reference counts and expecting them to match with actual executing code doesn't work well as there are often references you do not know about in compiled code. For this reason you will see many writers discouraging tracking reference counts.



          Instead think in terms of strong references making an ownership stake in an object, and as long as there is an owner the object survives.



          This might seem to be essentially the same thing, but there is a subtle difference in how the two are viewed: the reference count belongs to the object; while the strong reference attribute, which asserts an ownership stake, is part of the belongs to the referencing variable.



          In your comment example a has an ownership stake in the CountryModel object it references - the only ownership stake you have so far created to that object. The CountryModel object has ownership stakes in the objects that country and dialcode reference, but may not be the only such stakeholders – in that fragment we've no idea what other reference variables assert an ownership stake in the objects those two variables reference.



          Later when you create temparray the object reference stored in a is copied into the array and temparray claims and ownership stake in the referenced CountryModel object – while a continues to assert its own ownership stake as well. So now you have two variables you have created asserting an ownership stake in the CountryModel object.



          A similar explanation applies to b & c.



          When there is no ownership stake claimed by any variable/property over an object then that object can be destroyed. As part of that destruction any variables/properties that are part of that object are destroyed and any ownership stakes they asserted over references stored in them are withdrawn.



          If that process results in more objects having no more ownership stakes asserted over them then those objects too can be destroyed; the process continuing to destroy unwanted objects until the only remaining objects are those where some variable/property asserts an ownership stake over them.



          HTH more than it confuses!






          share|improve this answer















          Simple answer: automatically (provided you haven't disable ARC)



          Slightly longer answer (provided you haven't disabled ARC):



          When you assign values to the properties country and dialCode of an instance of CountryModel then that instance will have strong references to the referenced NSString values.



          If you assign a new value to either of these properties then the previous strong reference will be dropped and replaced by a strong reference to the new value.



          When the instance of CountryModel is no longer strongly reference by anything then the strong references it contains will be dropped.



          When a strong reference is dropped if there are no other strong references to the reference object then that object becomes reclaimable and the memory it occupies returned to the available memory pool.



          Note: if an NSString reference refers to a literal string then the literal string itself is immortal and will never be reclaimed. If you are trying to gian an understanding of when memory is released by monitoring memory usage, tracking dealloc calls, etc. do not use literal strings as your test objects – it is best to use a user-defined type.



          But I've disabled ARC... Then memory will get reclaimed when you manually instruct it to be. If you're getting leaks turning on ARC is your best choice, if you cannot then you've some debugging to do!



          HTH



          Addendum after comment



          Trying to follow reference counts and expecting them to match with actual executing code doesn't work well as there are often references you do not know about in compiled code. For this reason you will see many writers discouraging tracking reference counts.



          Instead think in terms of strong references making an ownership stake in an object, and as long as there is an owner the object survives.



          This might seem to be essentially the same thing, but there is a subtle difference in how the two are viewed: the reference count belongs to the object; while the strong reference attribute, which asserts an ownership stake, is part of the belongs to the referencing variable.



          In your comment example a has an ownership stake in the CountryModel object it references - the only ownership stake you have so far created to that object. The CountryModel object has ownership stakes in the objects that country and dialcode reference, but may not be the only such stakeholders – in that fragment we've no idea what other reference variables assert an ownership stake in the objects those two variables reference.



          Later when you create temparray the object reference stored in a is copied into the array and temparray claims and ownership stake in the referenced CountryModel object – while a continues to assert its own ownership stake as well. So now you have two variables you have created asserting an ownership stake in the CountryModel object.



          A similar explanation applies to b & c.



          When there is no ownership stake claimed by any variable/property over an object then that object can be destroyed. As part of that destruction any variables/properties that are part of that object are destroyed and any ownership stakes they asserted over references stored in them are withdrawn.



          If that process results in more objects having no more ownership stakes asserted over them then those objects too can be destroyed; the process continuing to destroy unwanted objects until the only remaining objects are those where some variable/property asserts an ownership stake over them.



          HTH more than it confuses!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 21:02

























          answered Nov 13 '18 at 14:38









          CRDCRD

          44.8k44870




          44.8k44870












          • Thanks for answering...from where do you got that countycode instance?

            – Sunil aruru
            Nov 13 '18 at 18:41












          • Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

            – Sunil aruru
            Nov 13 '18 at 18:53












          • @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

            – CRD
            Nov 13 '18 at 20:36











          • @Sunilaruru - see addendum

            – CRD
            Nov 13 '18 at 21:02

















          • Thanks for answering...from where do you got that countycode instance?

            – Sunil aruru
            Nov 13 '18 at 18:41












          • Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

            – Sunil aruru
            Nov 13 '18 at 18:53












          • @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

            – CRD
            Nov 13 '18 at 20:36











          • @Sunilaruru - see addendum

            – CRD
            Nov 13 '18 at 21:02
















          Thanks for answering...from where do you got that countycode instance?

          – Sunil aruru
          Nov 13 '18 at 18:41






          Thanks for answering...from where do you got that countycode instance?

          – Sunil aruru
          Nov 13 '18 at 18:41














          Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

          – Sunil aruru
          Nov 13 '18 at 18:53






          Let a = modelclass(country, dialcode) / Let b = modelclass(country, dialcode)/ Let c = modelclass(country, dialcode) / let temparray = [a,b,c] now please write reference counting for each step?

          – Sunil aruru
          Nov 13 '18 at 18:53














          @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

          – CRD
          Nov 13 '18 at 20:36





          @Sunilaruru - oops, fixed the CountryCode type, now correctly says CountryModel.

          – CRD
          Nov 13 '18 at 20:36













          @Sunilaruru - see addendum

          – CRD
          Nov 13 '18 at 21:02





          @Sunilaruru - see addendum

          – CRD
          Nov 13 '18 at 21:02

















          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%2f53280457%2fhow-memory-management-will-work-in-model-classswift-or-objective-c%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3