0

OK, I understand that I can not modify the results of a Realm Object.

So what is best way to change the data.

First I get all the Realm data as Results< Month >

let m = Month.getAllEntriesByDateAsc()

Now I need to loop through all the data to modify it. (This is a function to recalculate the entire table data.)

So I want to loop through the data and do something like:

for i in m {

   var d = i

   // perform calculations like
   d.value = 9999 

}

I want to do all the modifying on d.

Is these some sort of mapping I can use to create the new edible object from the Realm data?

Previously I did something like this:

for i in m {

    let d = Month()
    d.value = i.value
    d.status = i.status

}

But there are now to many variables.

I guest what I need to so change the Realm Object to the Model object?

And the .toArray() stuff will not work inside the loop? Not sure why.

Thanks.

diogenes
  • 1,865
  • 3
  • 24
  • 51
  • What's wrong with your for loop? You should probably put it inside a `write` block, but other than that, it seems perfectly fine. – Sweeper Nov 06 '19 at 06:46
  • *can not modify the results of a Realm Object.* is not correct. Results contain Realm Objects - those can be easily modified inside a write loop. Also, this is not a calculation `d.value = 9999* it's an assignment and if d not not managed by realm, it could be modified outside a write loop as well. The question is a bit vague and we don't know what your Realm object looks like so any answers are currently a guess. – Jay Nov 06 '19 at 17:54
  • I just used the "9999" for example. I just need to find a way to get the information into a new model object so the data can be modified and being free from Realm is ok. I need to go from Results into a Month() in a loop. thanks for you help. – diogenes Nov 06 '19 at 23:16
  • is there just someway to do a loop (map) with key values? that would be fine also. – diogenes Nov 06 '19 at 23:19
  • You need to clarify the question! What do your Realm models look like? You can easily make non-managed copies of your Realm objects that can be modified but again, understanding what the models look like will help us understand what you're trying to do so we can formulate an answer. Also, when responding here in comments, please respond with an @ symbol - that will notify us that you responded. Like @diogenes for example. – Jay Nov 07 '19 at 19:44

1 Answers1

-1
extension Results {
    func toArray<T>(ofType: T.Type) -> [T] {
        var array = [T]()
        for i in 0 ..< count {
            if let result = self[i] as? T {
                array.append(result)
            }
        }

        return array
    }
}

From here

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • I already tried that but it does not work on the "d". I have that for the "m" but I need to do it inside the loop if possible? – diogenes Nov 06 '19 at 05:49
  • @diogenes you have to provide examples of what have you tried and what error you get. – Evgeniy Kleban Nov 06 '19 at 05:51
  • it will not let me do this: var d = i.toArray(Month). I get "Value of type 'Month' has no member 'toArray'" error – diogenes Nov 06 '19 at 05:55
  • @diogenes You probably do not want to do what's shown in this answer. Casting Results to an Array breaks the connection between the object and Realm and you will no longer receive events or notifications about those objects. They may also loose their ordering and a number of other things. Additionally, you can replace all of that code with one line `let myArray = Array(realmResults)` – Jay Nov 06 '19 at 17:56
  • I tried to just do an array but get error: Generic parameter 'Element' could not be inferred. Initializer 'init(_:)' requires that 'Month' conform to 'Sequence' – diogenes Nov 06 '19 at 23:12
  • @diogenes Is Month a Realm Object? If so it wouldn't make sense to try to make a realm object an array which is why you got that error; Month is a class and not something that conforms to a sequence. – Jay Nov 07 '19 at 16:11