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!
ios swift tableview observers
add a comment |
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!
ios swift tableview observers
add a comment |
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!
ios swift tableview observers
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
ios swift tableview observers
edited Nov 9 at 5:18
asked Nov 9 at 4:55
arvnq
1314
1314
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53220080%2fswift-simultaneous-accesses-to-0x10959c600-but-modification-requires-exclusiv%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