20615

updating an array of items in realm swift

Question:

I am trying to update the content of a real database which takes an array. I would like to update the array stored in it below is my code for it

class TodoListModel: Object { @objc dynamic var id = UUID().uuidString let photos = List<Data>() @objc dynamic var createdDate: Date? override static func primaryKey() -> String? { return "id" } let parentCategory = LinkingObjects(fromType: CategoryModel.self, property: "items") }

In this case, the new Data just gets added instead on replacing the previous one

func updateTodoList(update: TodoListModel, createdDate: Date, photo: Array<Data>) -> Void { update.createdDate = createdDate update.photo.append(objectsIn: photo) }

Answer1:

If you want to remove all existing elements of TodoListModel.photos in your updateTodoList method, you simply need to call update.photos.removeAll before appending the contents of the photos input argument to it.

func update(todoList: TodoListModel, createdDate: Date, photos: Array<Data>) { todoList.createdDate = createdDate todoList.photos.removeAll() todoList.photos.append(objectsIn: photos) }

P.S.: I've also renamed your function and its input arguments to match the Swift naming convention and the data that each input argument actually represents. There's also no need to write out the return value if your function is returning Void.

Recommend

  • Use of XSD Duration type, using XSLT to out put value to normal duartion format
  • Order By not working in Azure Web Document Explorer
  • Deserialize to Object with a List
  • Getting error: Use of unresolved identifier “LinkingObjects”
  • In IE 8 dates are NaN/Nan/Nan
  • Getting CouchDB to work with node.js
  • RecyclerView Dynamic Header
  • Auto Height of UICollectionView inside UITableViewCell
  • Convert Func to Func
  • Custom Nav Title offset ios 11
  • Functions in global context
  • Update CALayer sublayers immediately
  • Illegal mix of collations for operation for date/time comparison
  • JFileChooser in front of fullscreen Swing application
  • sending/ receiving email in Java
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to delete a row from a dynamic generate table using jquery?
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • Hits per day in Google Big Query
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?