-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support storing arrays of primitives (string, int, etc.) as RLMObject properties #1120
Comments
Hi @andrewjl, the reason There are two fundamental ways we could add support for primitive types in Realm.
So although we agree it'd be nice to store |
Thanks @jpsim for the explanation. I can see how option 2 would be complex to implement. Option 1 seems more doable, would you be willing to consider a pull request implementing it for arrays of |
I can't speak for the rest of the team, but I'd be open to that. |
+1 |
@jpsim based on our chat in person (at the carthage event) it sounds like the best solution would be to do what Realm already requires the user to do automatically, and transparently. Seems like the best place to do this would be in Let me know if you think this is in the right direction. Also happy to provide more details if this is too high level. |
More thoughts... when we detect a white listed inner
Update: After looking through some more classes, I noticed that |
I'm a little late to the party but I'd like to offer some thoughts. Regarding option 2, that is changing Realm's modeling approach to be protocol based over subclassing, is in my opinion the far superior approach. While it might require more work on the side of the Realm development team, long term it makes more sense because it's far more extensible. In fact one of the few things things I don't like about Realm (it's fantastic overall) is the limited amount of types that can represented, say for example a list of strings. Having to wrap use cases like that in a whole separate class is messy, something I'm sure fans of code style and beauty would shudder at. A protocol based approach is actually how Mantle does it as of their 2.0 release. It's the only reason that I can use Realm and Mantle together. Mantle doesn't just use a protocol though; they provide a base class of Under the hood I'd imagine that the modeling approach would be purely protocol based; the And finally, |
i do it like this if i want to store simple types #import "Contributor.h" @interface Contributor() @property (nonatomic, strong) NSData * spectrumPeaksData; @end @implementation Contributor + (nullable NSDictionary *)defaultPropertyValues { return @{@"spectrumPeaksData":[NSData data]}; } //////////////////////////////////////////////////////////////////////// #pragma mark - Peaks //////////////////////////////////////////////////////////////////////// - (void)setSpectrumPeaks:(NSArray *)spectrumPeaks { if ([spectrumPeaks count] == 0) { self.spectrumPeaksData = [NSData data]; return; } self.spectrumPeaksData = [NSKeyedArchiver archivedDataWithRootObject:spectrumPeaks]; } - (NSArray *)spectrumPeaks { if ([self.spectrumPeaksData length] == 0) { return nil; } return [NSKeyedUnarchiver unarchiveObjectWithData:self.spectrumPeaksData]; } @end alternatively you can cache the unarchive object until you call the set method again, where you invalidate the cache #import "Contributor.h" @interface Contributor() @property (nonatomic, strong) NSData * spectrumPeaksData; @property (nonatomic, strong) NSArray * spectrumPeaksCache; @end @implementation Contributor + (nullable NSDictionary *)defaultPropertyValues { return @{@"spectrumPeaksData":[NSData data]}; } + (NSArray *)ignoredProperties { return @[@"spectrumPeaksCache"]; } //////////////////////////////////////////////////////////////////////// #pragma mark - Peaks //////////////////////////////////////////////////////////////////////// - (void)setSpectrumPeaks:(NSArray *)spectrumPeaks { self.spectrumPeaksCache = nil; if ([spectrumPeaks count] == 0) { self.spectrumPeaksData = [NSData data]; return; } self.spectrumPeaksData = [NSKeyedArchiver archivedDataWithRootObject:spectrumPeaks]; } - (NSArray *)spectrumPeaks { if ([self.spectrumPeaksData length] == 0) { return nil; } self.spectrumPeaksCache = [NSKeyedUnarchiver unarchiveObjectWithData:self.spectrumPeaksData]; return self.spectrumPeaksCache; } @end |
But don't you think that that's an incredible amount of code just to achieve the simple task of storing primary data types? It seems so counter to the point. |
yes, you are right... but i list the code as only the current solution |
What exactly are you try to achieve here, an array of |
nope, an array of any object that can be archived (NSString, NSData, NSDate, NSNUmber, NSDictionary etc) see
|
I have a need to store arrays of Ints as Object properties. Thanks @peterpaulis for your code sample above. I do agree that is a lot of code for something that seems commonplace and expected. Although explicit support for collections of primitive types would be great, it seems to me a better fallback position would be to have some sort of a general mechanism for property transformations. Most other ORMs will allow you to define 2 way transforms for "custom" property types. Another idea would be to create something like a JSON primitive type that could automatically be transformed to/from a Dictionary or Array. Still, these are both just catchalls, for when the framework can't handle something different. I still would expect collections of primitives to be something that is supported out of the box. BTW - I'm working on switching my project from couchbase-lite to realm. Array properties were a big source of difficulty for me on couchbase too. |
+1 |
1 similar comment
+1 |
+1, It's very bad without this feature. I have two classes which have string arrays. How can I do if I want to delete one class from the database since realm does not support cascading delete? All string are stored in another table. |
+1000 |
I would love to help get this out sooner. @timanglade @jpsim I have some familiarity with the objective-c part of the codebase. If I can get some guidance as to where to start and what approach to take (at a high level) I'm happy to get the ball rolling on this. |
|
+1 |
+infinite |
+over 9000 |
Anyone knows how it works on Android? |
@jazz-mobility What does the |
@dmdque he is probably using this library here: https://github.com/Hearst-DD/ObjectMapper |
I see. Thanks! |
Yes, I was using Realm + ObjectMapper. |
This doesn't seem to have been fixed. What's the priority of this? |
The priority is "S:P2 Backlog", which is just behind "S:P1 Backlog". This is publicly reflected in the GitHub issue labels. This is being designed and worked on currently in realm-core, but I can't share any concrete estimates of when we might officially release that functionality. |
+1 |
@jazz-mobility We can also write transforms for ObjectMapper:- class RealmString: RLMObject {
dynamic var stringValue : String = ""
}
// In mapping method
cities <- (map["cities"], stringListTransform)
let stringListTransform = TransformOf<RLMArray, [String]>(fromJSON: { (value: [String]?) -> RLMArray? in
let realmArray = RLMArray(objectClassName: "RealmString")
guard let items = value else { return realmArray }
for item in items {
let realmString = RealmString()
realmString = item
realmArray(realmString)
}
return realmArray
}, toJSON: { (value: RLMArray?) -> [String]? in
return nil
}) |
@aksswami If you check the description, it is clearly mention "Support storing arrays of primitives". I guess this transformation is supported from long time now. But this is not the real solution to the problem. |
+1 |
The question is: |
We are currently working on this feature, as the |
Arrays of primitives are supported as of v3.0.0-beta.4. |
Reading over issue #1028 it looks like RLMObject doesn't support properties that are arrays of non-RLMObjects. Instead the individual strings each have to be declared as a property of an RLMObject and then stored in an RLMArray.
I'm curious to understand why your support for primitives doesn't extend to collections of objects of those types. As far as Realm is concerned, what's the difference between storing a string or an array of strings? And would you ever add this support to a future release?
Subtasks:
The text was updated successfully, but these errors were encountered: