RLMResults bridge to NSArray (iOS)

3,231 views
Skip to first unread message

Oleg Anghelov

unread,
Feb 24, 2015, 6:09:45 AM2/24/15
to realm...@googlegroups.com
Hi everyone.
I spent some time digging into the topic, but with no success.
I see that the only collection type RLM is working with is RLMResults.

I found no way of just getting an NSArray of objects.

Is there a specific reason RLM has not this feature?


If anybody wonders why NSArray is needed, I would say it's for the abstractization purposes, where I just don't need the power and the specific behaviour of RLMResults and just have a simple NSArray instead.

Thank you in advance!

Best regards,
Oleg Anghelov

Samuel Giddins

unread,
Feb 24, 2015, 12:55:56 PM2/24/15
to Oleg Anghelov, realm...@googlegroups.com
Oleg,
My personal recommendation when you only need to represent a sequence type would be to deal with protocols, rather than require a concrete class. For instance, both NSArray and RLMResults conform to the NSFastEnumeration protocol (and the SequenceType protocol in Swift). There's a huge downside to converting your RLMResults to an NSArray, since the objects returned from the results are lazily created, and thus have a much smaller overhead. If you still wish to convert your RLMResults to an NSArray, however, you can do it with the following code snippet:

NSArray *RLMResultsToNSArray(RLMResults *results) {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:results.count];
    for (RLMObject *object in results) {
        [array addObject:object];
    }
    return array;
}


I hope that helps!

--
Samuel Giddins



--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/f6055225-b9a7-44fb-a182-131a0a31d8ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:73637962-570#}

Oleg Anghelov

unread,
Feb 27, 2015, 5:01:18 AM2/27/15
to realm...@googlegroups.com, oleg.a...@gmail.com, he...@realm.io
Samuel,
Thank you for your detailed answer!
Before posting this to this topic I did something similar to your code snippet, but doing so I do feel that I am doing it in the wrong way :)
I am trying now to get a better understanding of what you said, but I feel that still need more details...
I really hope you will find couple more minutes to describe
 
There's a huge downside to converting your RLMResults to an NSArray, since the objects returned from the results are lazily created, and thus have a much smaller overhead.

 The first obvious downside, that each time we to a fetch, it will then waste some computation time on generating a new array. But what did you mean by that the objects from NSArray would have a smaller overhead?

Samuel Giddins

unread,
Feb 28, 2015, 1:14:27 PM2/28/15
to Oleg Anghelov, realm...@googlegroups.com
Oleg,
I meant that getting objects from an RLMCollection will have less overhead, as memory will be allocated only once you attempt to access the objects in the collection.

--
Samuel Giddins



{#HS:73637962-570#}
Message has been deleted

Samuel Giddins

unread,
Mar 2, 2015, 1:32:28 PM3/2/15
to Oleg Anghelov, realm...@googlegroups.com
RLMCollection implements -objectForIndexedSubscript, so you can indeed do something like object = self.names[index];. Hope that helps!

--
Samuel Giddins



On Mon, Mar 2, 2015 at 4:08 PM UTC, Oleg Anghelov <oleg.a...@gmail.com> wrote:
Ah, ok, I misread and thought you are referring the lazy-creation and the
small overhead to the NSArray creation.
Now I got your point right.
Thanks for sharing your thoughts :)

And the very last thing on this topic:
Not sure if objective-c would allow it, but is it possible to give to
RLMResults' elements the possibility to be addressed in literal style? So
instead of
[self.names objectAtIndex:i]
we can use
self.names
?
{#HS:73637962-570#}

Oleg Anghelov

unread,
Sep 8, 2015, 6:03:05 AM9/8/15
to Realm
Can we come back to the topic for a while?

It's getting more and more popular to do developing in a nicely-architectured way. 

One of the best examples would be Robert C. Martin videos at CleanCoders, that opt for modular approach, where a DB or Networking library is rather a plugin and a module, that is well abstracted in the app, so there are no library dependency and it can be easily replaced.

In our app the results we are working with are pretty small, a matter of dozens, and getting to hundreds in really really rare cases. 

But I see in the future an app that will handle thousands and thousands of results on a single fetch. And imagining doing a "for in" to transfer it to NSArray would be a striking blow to app performance.
But at the same time, keeping the libraries abstracted, and using only data structures in the app instead of DB-objetcs, would get impossible.

Is there anything Realm is going to do about it?
Or if the architecture of the project chooses to go with Boundary-Interactor-Entity approach, there is no place for Realm?

Samuel Giddins

unread,
Sep 8, 2015, 2:12:09 PM9/8/15
to Oleg Anghelov, realm...@googlegroups.com
Hi Oleg,
Realm does its best to feel at home in the Cocoa world, whether in Swift or Objective-C.
In Swift, results can already be used through their conformance to the CollectionType protocol, which Array (as well as Set, Dictionary, Sting.CharacterView, etc) conforms to.
In Objective-C, there's really no native protocol (besides NSFastEnumeration, which RLMResults already conforms to) that describes what it means to be a collection-like object. You're free to come up with your own protocol type, make an NSArray subclass that proxies to an RLMResult, or simply cast the RLMResults to an NSArray -- the interfaces are nearly 100% compatible.

--
Samuel Giddins



--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa unsub...@googlegroups.com.

To post to this group, send email to



{#HS:118740376-1815#}
Reply all
Reply to author
Forward
0 new messages