Kashish Goel Kashish Goel - 1 year ago 121
Swift Question

Deleting specific Object Realm Swift

I've gone through Realm's documentation and I've looked online but didn't find any solution to my specific problem.

Here is what I have:

My app is storing specific information into various folders, the user can store the same Object in multiple folders or what so ever.

Here is the folder object, it has a one to many relations with items. Folders can store as many items as they want.

class Folder: Object {

dynamic var name = ""
dynamic var lastEdited = NSDate()
dynamic var folderDescription = ""
let items = List<ItemModel>()


The item model:

class ItemModel: Object {

dynamic var country:String!
dynamic var date:String!
dynamic var title:String!
dynamic var area:String!
dynamic var type:String!
dynamic var text:String!
dynamic var registered:String!
dynamic var id:String!
let metamap = List<Meta>()
let authors = List<Manu>()
let categories = List<Category>()


The user can store as many of these items in folders, they can add the same Item to multiple folders as well. For example the user can add the same item to the folder saved, bookmarks and wish list.

The problem I'm having is, when a user goes to delete an item from a folder, it deletes it from every other folder. E.g. I want to only delete a specific item from the saved folder, but for some reason it also deletes it from the bookmarks and wish list folder.

Here is what I am using to delete the folder:

let folderItemToDelete = folder?.items[indexPathToDelete!]
try! uiRealm.write {


I parse into a specific folder name and am trying to delete a folder at a specific index, but for some reason they all get deleted. What can I do so that it only deletes it from the specific folder?

Answer Source

By calling Realm.delete(_:) you're deleting the object itself from the Realm. From your description you only want to remove the reference to it from a single List. You can do this using List.removeAtIndex(_:).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download