Swift : Simultaneous accesses to 0x10959c600, but modification requires exclusive access on Structs











up vote
1
down vote

favorite












I want to understand why using a struct is causing this issue and a class does not. I encountered the issue when appending elements to array and having an observer load a table. Here are the codes:



Order.swift - items ordered



struct Order {
var menuItems: [MenuItem]
init(menuItems: [MenuItem] = ) {
self.menuItems = menuItems
}
}


MenuController.swift - contains the order and a shared instance



struct MenuController {  
static var shared: MenuController = MenuController()
static let orderNotification = Notification.Name("MenuController.orderUpdated")

var order = Order() {
didSet {
NotificationCenter.default.post(name: MenuController.orderNotification,
object: nil)
}
}


MenuItemViewController.swift - ordering screen



@IBAction func orderButtonTapped(_ sender: UIButton) {
MenuController.shared.order.menuItems.append(menuItem)
}


OrderTableViewController.swift - items ordered screen



override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(tableView,
selector: #selector(UITableView.reloadData),
name: MenuController.orderNotification, object: nil)
}
...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MenuController.shared.order.menuItems.count //Simultaneous access error thrown here
}


Now, I made a design mistake where MenuController should be a class instead of a struct because we're gonna be sharing MenuController instance throughout the app hence it should be a reference type. Using a class solved the simultaneous access issue.



What I am confused about is :
Why is a value-typed (struct) MenuController causing this simultaneous access issue when observer try to load the tableView.



Hope someone can explain. TIA!










share|improve this question




























    up vote
    1
    down vote

    favorite












    I want to understand why using a struct is causing this issue and a class does not. I encountered the issue when appending elements to array and having an observer load a table. Here are the codes:



    Order.swift - items ordered



    struct Order {
    var menuItems: [MenuItem]
    init(menuItems: [MenuItem] = ) {
    self.menuItems = menuItems
    }
    }


    MenuController.swift - contains the order and a shared instance



    struct MenuController {  
    static var shared: MenuController = MenuController()
    static let orderNotification = Notification.Name("MenuController.orderUpdated")

    var order = Order() {
    didSet {
    NotificationCenter.default.post(name: MenuController.orderNotification,
    object: nil)
    }
    }


    MenuItemViewController.swift - ordering screen



    @IBAction func orderButtonTapped(_ sender: UIButton) {
    MenuController.shared.order.menuItems.append(menuItem)
    }


    OrderTableViewController.swift - items ordered screen



    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(tableView,
    selector: #selector(UITableView.reloadData),
    name: MenuController.orderNotification, object: nil)
    }
    ...
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return MenuController.shared.order.menuItems.count //Simultaneous access error thrown here
    }


    Now, I made a design mistake where MenuController should be a class instead of a struct because we're gonna be sharing MenuController instance throughout the app hence it should be a reference type. Using a class solved the simultaneous access issue.



    What I am confused about is :
    Why is a value-typed (struct) MenuController causing this simultaneous access issue when observer try to load the tableView.



    Hope someone can explain. TIA!










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to understand why using a struct is causing this issue and a class does not. I encountered the issue when appending elements to array and having an observer load a table. Here are the codes:



      Order.swift - items ordered



      struct Order {
      var menuItems: [MenuItem]
      init(menuItems: [MenuItem] = ) {
      self.menuItems = menuItems
      }
      }


      MenuController.swift - contains the order and a shared instance



      struct MenuController {  
      static var shared: MenuController = MenuController()
      static let orderNotification = Notification.Name("MenuController.orderUpdated")

      var order = Order() {
      didSet {
      NotificationCenter.default.post(name: MenuController.orderNotification,
      object: nil)
      }
      }


      MenuItemViewController.swift - ordering screen



      @IBAction func orderButtonTapped(_ sender: UIButton) {
      MenuController.shared.order.menuItems.append(menuItem)
      }


      OrderTableViewController.swift - items ordered screen



      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(tableView,
      selector: #selector(UITableView.reloadData),
      name: MenuController.orderNotification, object: nil)
      }
      ...
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return MenuController.shared.order.menuItems.count //Simultaneous access error thrown here
      }


      Now, I made a design mistake where MenuController should be a class instead of a struct because we're gonna be sharing MenuController instance throughout the app hence it should be a reference type. Using a class solved the simultaneous access issue.



      What I am confused about is :
      Why is a value-typed (struct) MenuController causing this simultaneous access issue when observer try to load the tableView.



      Hope someone can explain. TIA!










      share|improve this question















      I want to understand why using a struct is causing this issue and a class does not. I encountered the issue when appending elements to array and having an observer load a table. Here are the codes:



      Order.swift - items ordered



      struct Order {
      var menuItems: [MenuItem]
      init(menuItems: [MenuItem] = ) {
      self.menuItems = menuItems
      }
      }


      MenuController.swift - contains the order and a shared instance



      struct MenuController {  
      static var shared: MenuController = MenuController()
      static let orderNotification = Notification.Name("MenuController.orderUpdated")

      var order = Order() {
      didSet {
      NotificationCenter.default.post(name: MenuController.orderNotification,
      object: nil)
      }
      }


      MenuItemViewController.swift - ordering screen



      @IBAction func orderButtonTapped(_ sender: UIButton) {
      MenuController.shared.order.menuItems.append(menuItem)
      }


      OrderTableViewController.swift - items ordered screen



      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(tableView,
      selector: #selector(UITableView.reloadData),
      name: MenuController.orderNotification, object: nil)
      }
      ...
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return MenuController.shared.order.menuItems.count //Simultaneous access error thrown here
      }


      Now, I made a design mistake where MenuController should be a class instead of a struct because we're gonna be sharing MenuController instance throughout the app hence it should be a reference type. Using a class solved the simultaneous access issue.



      What I am confused about is :
      Why is a value-typed (struct) MenuController causing this simultaneous access issue when observer try to load the tableView.



      Hope someone can explain. TIA!







      ios swift tableview observers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 5:18

























      asked Nov 9 at 4:55









      arvnq

      1314




      1314





























          active

          oldest

          votes











          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',
          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%2f53220080%2fswift-simultaneous-accesses-to-0x10959c600-but-modification-requires-exclusiv%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220080%2fswift-simultaneous-accesses-to-0x10959c600-but-modification-requires-exclusiv%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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff