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

BETA! support for NULL string & binary properties #628

Closed
heardrwt opened this Issue Jul 21, 2014 · 141 comments

Comments

Projects
None yet
@heardrwt

heardrwt commented Jul 21, 2014

This would allow for the storage of prop = [NSData data] and prop = nil;

@alazier

This comment has been minimized.

Contributor

alazier commented Jul 21, 2014

This is definitely something we want. We are currently waiting for support for optional values in core at which point we wil add support in the objc binding. @astigsen can give a better estimate of when this will happen.

@elado

This comment has been minimized.

elado commented Jul 28, 2014

👍 sounds really important. Especially with NSStrings -- nullable values are common.

@jeremiegirault

This comment has been minimized.

jeremiegirault commented Aug 6, 2014

👍 I'd like to hear when this will happen

@alazier

This comment has been minimized.

Contributor

alazier commented Aug 6, 2014

This is high on the list of features to add. It's a bit tricky though, as this requires us to add an extra bit to indicate if a property is NULL in the core storage engine making this change non-trivial. Will be sure to update you guys once we have made progress or have a more concrete timeline.

@komocode

This comment has been minimized.

komocode commented Aug 11, 2014

👍 argh! I made the switch from FMDB too fast without realizing the limitations of realm! This is desperately needed ASAP as I'm synchronizing with CloudKit and optional fields would definitely be a huge help.

@bmunkholm

This comment has been minimized.

Contributor

bmunkholm commented Aug 11, 2014

You can work around this limitation by adding that extra bit of information
in a boolean property "myPropertyIsNull" or similar.

On Mon, Aug 11, 2014 at 3:15 AM, komocode notifications@github.com wrote:

[image: 👍] argh! I made the switch from FMDB too fast without
realizing the limitations of realm! This is desperately needed ASAP as I'm
synchronizing with CloudKit and optional fields would definitely be a huge
help.


Reply to this email directly or view it on GitHub
#628 (comment).

@alazier

This comment has been minimized.

Contributor

alazier commented Aug 11, 2014

Another option is to wrap your property in an RLMObject which can be set to nil. For instance for a double, you could make an object:

@interface DoubleObject : RLMObject 
@property double value;
@end

You could then set instances of this object to nil, with the added complexity of having to unwrap your value every time you need to access it.

@komocode

This comment has been minimized.

komocode commented Sep 25, 2014

still nothing on this?
This problem is causing 99% of all of my Realm crashes.

@alazier

This comment has been minimized.

Contributor

alazier commented Sep 25, 2014

This is still near the top of the priority list along with fine grained notifications. Unfortunately neither of these features is trivial and still require quite a bit more work in the core library before we can add support to the objc apis.

I think most users who need this functionality are wrapping values in an RLMObject, which is the only type which supports nil values. For instance for an NSString you could define an object class:

@interface OptionalString : RLMObject
@property NSString *stringValue;
@end

The downside with this is the need to wrap/unwrap your value on each access.

@komocode

This comment has been minimized.

komocode commented Sep 25, 2014

Got it. Though, I think I'm going to have to wait until enhancement is in, that's a lot of work to wrap it around.

@eni9889

This comment has been minimized.

eni9889 commented Oct 15, 2014

+1 for this. It can't come soon enough. What's the progress look like for this?

@javierjulio

This comment has been minimized.

javierjulio commented Oct 15, 2014

My guess is its best to avoid nil where possible. While I needed it, I can work around it with existing data I have and in fact using that data makes querying clearer. I won't have to worry about nils now. Still though would be useful to have this. Thought I was doing something wrong since I didn't think this wasn't supported but I understand its a tough problem. Most likely worthwhile to mention in the docs if it isn't.

@zvonicek

This comment has been minimized.

zvonicek commented Oct 15, 2014

Yeah, this is really needed. Now I have to work it around by values which I consider to be null, for example NSDate(timeIntervalSince1970: 1).

@Bruno-Furtado

This comment has been minimized.

Bruno-Furtado commented Oct 16, 2014

I tried to use the following option but did not work:

@interface OptionalString : RLMObject
@property NSString *stringValue;
@end

I opted to check the optional fields when using a dictionary:

@implementation ObjectA

- (instancetype)initWithObject:(id)object
{
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *mObject = [object mutableCopy];

        if (object[@"key"] == nil || [object[@"key"] isKindOfClass:[NSNull class]]) {
            mObject[@"key"] = @"";
        }

        object = mObject;
    }

    return [super initWithObject:object];
}

@end
@GreatApe

This comment has been minimized.

Contributor

GreatApe commented Oct 16, 2014

What was it that didn't work about the OptionalString object? It should work, so if you are ok with the inconvenience it could be a good workaround. You would need to set the property's value to [NSNull null] manually, though.

@Bruno-Furtado

This comment has been minimized.

Bruno-Furtado commented Oct 16, 2014

@GreatApe, example:

JSON

{
"youtube_id" : "EGfaTllBlHI"
}

NSDictionary

{
"youtube_id" = EGfaTllBlHI;
}

Models

#import "RLMObject.h"
#import "OptionalString.h"

@interface Recipe : RLMObject
@property OptionalString *youtube_id;
@end
#import "RLMObject.h"

@interface OptionalString : RLMObject
@property NSString *stringValue;
@end

Error

When youtube_id is nil, works correctly.
When the youtube_id is not nil it shows this error.

2014-10-16 09:08:04.782 App[78352:8924122] *** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value 'nil' for property 'stringValue''
@GreatApe

This comment has been minimized.

Contributor

GreatApe commented Oct 16, 2014

You need to expand this a little be so I can see how you create the RLMObjects, but generally speaking I think you might be using initWithObject as if it were a recursive parser.

@Bruno-Furtado

This comment has been minimized.

Bruno-Furtado commented Oct 16, 2014

@GreatApe, thanks!

This works:

@implementation Recipe

- (instancetype)initWithObject:(id)object
{
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *mObject = [object mutableCopy];

        if ([mObject[@"youtube_id"] isKindOfClass:[NSNull class]] == NO) {
            mObject[@"youtube_id"] = @{@"stringValue" : mObject[@"youtube_id"]};
        }

        object = mObject;
    }

    return [super initWithObject:object];
}

@end
@GreatApe

This comment has been minimized.

Contributor

GreatApe commented Oct 16, 2014

Glad it works!

@jpsim jpsim closed this Oct 16, 2014

@komocode

This comment has been minimized.

komocode commented Oct 16, 2014

this is closed? optionals are in realm now?

@jpsim jpsim reopened this Oct 16, 2014

@jpsim

This comment has been minimized.

Contributor

jpsim commented Oct 16, 2014

@komocode sorry to get your hopes up, I mistakenly closed this.

@mekanics

This comment has been minimized.

mekanics commented Nov 7, 2014

👍
any progress on this?

@jpsim

This comment has been minimized.

Contributor

jpsim commented Nov 7, 2014

We consider this a high priority and have someone working on it. We'll update this thread when we have something to announce.

@anlaital

This comment has been minimized.

anlaital commented Nov 8, 2014

This is great news. Removes quite a bunch of hacks from our codebase at least. 👍

@remotevision

This comment has been minimized.

remotevision commented Aug 10, 2015

👍 glad to see this one is being test! I'd be happy to test as well.

@shawnwall

This comment has been minimized.

shawnwall commented Aug 11, 2015

What additional testing needs to be completed for the obj-c version to get this merged in? How can I help?

@timanglade

This comment has been minimized.

Contributor

timanglade commented Aug 11, 2015

We are fixing a few file format bugs we found through testing and have started adding support for the remaining types (int, float and all Cocoa types that derive from that). We’re anxious to deliver this too, and it looks like you won’t have to wait much longer. We’re not fully set yet on whether we’ll a) ship just string & byte support first, or b) all types at once. The former might be confusing and require you upgrade your app’s code in multiple passes. If we pick the former, this should be released very soon as we’re pretty much just waiting on bugfixes; if the latter it will take a bit longer as we’ll need to finalize support for the new types.

@i-schuetz

This comment has been minimized.

i-schuetz commented Aug 21, 2015

+1 for optional NSDate...

@komocode

This comment has been minimized.

komocode commented Aug 27, 2015

+1 for all types support.
I'm starting on a new project. Do you imagine support for all types will make it within the next month?

@bmunkholm

This comment has been minimized.

Contributor

bmunkholm commented Aug 27, 2015

We always try to stay away of promising any dates of feature releases. We want to have the flexibility to prioritise bugs and support above new features, which just makes it hard to predict feature releases. That said though, the core functionality for all types is now done (in the C++ Core), so we only got left to fully integrate this into the bindings. So yeah, if support doesn't get too crazy, I expect us to release support for nullability in all types within the next month - no promises though! :-)

@komocode

This comment has been minimized.

komocode commented Aug 27, 2015

Great! No worries if it's delayed, I just needed a ballpark number.

@AndrewBarba

This comment has been minimized.

AndrewBarba commented Aug 28, 2015

Hey if anyone is interested (until official support is merged), I ported AFNetworking's JSONObjectByRemovingKeysWithNullValues over to Swift 2.0 and it's been working really well for us:

public static func JSONObjectByRemovingKeysWithNullValues(JSONObject: AnyObject) -> AnyObject {
        switch JSONObject {
        case let JSONObject as [AnyObject]:
            return JSONObject.map { JSONObjectByRemovingKeysWithNullValues($0) }
        case let JSONObject as [String: AnyObject]:
            var mutableDictionary = JSONObject
            for (key, value) in JSONObject {
                switch value {
                case _ as NSNull:
                    mutableDictionary.removeValueForKey(key)
                default:
                    mutableDictionary[key] = JSONObjectByRemovingKeysWithNullValues(value)
                }
            }
            return mutableDictionary
        default:
            return JSONObject
        }
    }
@segiddins

This comment has been minimized.

Contributor

segiddins commented Aug 28, 2015

Support for nullable dates, ints, floats, doubles, and bools is being actively worked upon as I type this -- there's a pull request up at #2458 that adds this functionality for both Swift & Objective-C.

@AndrewBarba

This comment has been minimized.

AndrewBarba commented Aug 28, 2015

Awesome thanks for the update

@siuying

This comment has been minimized.

siuying commented Aug 28, 2015

@segiddins Any way I can use NULL String + Swift 2? Or I should use #2458 ?

@segiddins

This comment has been minimized.

Contributor

segiddins commented Aug 28, 2015

@siuying not yet, unfortunately. You won't be able to use #2458 yet because it requires a new release of our C++ core.

@siuying

This comment has been minimized.

siuying commented Aug 29, 2015

@segiddins thanks, I'd wait :)

@robertjpayne

This comment has been minimized.

robertjpayne commented Sep 7, 2015

👍 this is a huge enhancement and looking forward to it!

@pimnijman

This comment has been minimized.

pimnijman commented Sep 10, 2015

While we're waiting... here's my temporarily solution (in Swift). I'm hoping to easily migrate this to optionals when Realm is ready for it.

Instead of

dynamic public var myProperty: String?

I'll be using

dynamic public var myPropertyIsNil = true
dynamic public var myPropertyValue = ""
public var myProperty: String? {
    get {
        return (self.myPropertyIsNil) ? nil : self.myPropertyValue
    }
    set {
        self.myPropertyIsNil = (newValue == nil)
        self.myPropertyValue = newValue ?? ""
    }
}

Be sure to add "myProperty" to your ignoredProperties array.

@jessyconroy

This comment has been minimized.

jessyconroy commented Sep 18, 2015

Hi,

Any update on a NULL values Realm release for Swift 2.0? Xcode 7 went live the other day and I migrated my code from Swift 1.2 to 2.0. I'd say I'm not the only person who updated :)

@pravintate

This comment has been minimized.

pravintate commented Oct 6, 2015

realm swift 0.95.2 version
how to set Int double variable as optional. I don't want to set zero at declaration time as follow
dynamic var count : Int = 0

@kishikawakatsumi

This comment has been minimized.

Contributor

kishikawakatsumi commented Oct 6, 2015

@pravintate 0.95.2 and 0.95.3 does not support any null values yet. So you can workaround that the flag that indicate a value is null or wrap the value with Object. e.g. #1614 (comment)

Please wait releasing future version that support null values.

@pravintate

This comment has been minimized.

pravintate commented Oct 6, 2015

@kishikawakatsumi: If I set flag as per your suggestion then I will increase unnecessary coding for me and in few days Realm will support null that time again I need to update my code as per that. so using flag instead of null is not good option.

@timothyrobb

This comment has been minimized.

timothyrobb commented Oct 6, 2015

@pravintate

This comment has been minimized.

pravintate commented Oct 6, 2015

Ok thanks for replying
On 06-Oct-2015 2:27 pm, "Tim Robb" notifications@github.com wrote:

What they are saying, is that if you need support now, use a flag, like
many others have until now. Or just wait. They were responding to your
question.


Reply to this email directly or view it on GitHub
#628 (comment).

@jpsim

This comment has been minimized.

Contributor

jpsim commented Oct 14, 2015

We've officially released Realm Objective-C & Swift 0.96 with support for null values, keypath collection queries, RealmCollectionType & more! Read about it here: https://realm.io/news/realm-objc-swift-0.96.0/

Thank you all for your patience!

@jpsim jpsim closed this Oct 14, 2015

@jpsim jpsim added T:Feature and removed P1 T:Enhancement labels Oct 14, 2015

@yasirmturk

This comment has been minimized.

yasirmturk commented Nov 12, 2015

awesome changes the way we live 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment