1

I have stored messages in Firebase like so:

messageObject["timestamp"] = FIRServerValue.timestamp()

The objects have a child like: timestamp: 1465222757817. The problem is that older non 64 bit devices cannot handle Integers of that length. What would be a good workaraound for this problem?

Edit:

When declaring the timestamp as Int64, it throws an error:

var timestampQueryValue: Int64 = 1465222757817
self.chatRef.queryOrderedByChild("timestamp")
    .queryStartingAtValue(timestampQueryValue)
    .observeEventType(.ChildAdded, withBlock: { 
        (snapshot) -> Void in /* ... */ })

/* Error: Cannot convert value of type 'Int64' 
          to expected argument type 'AnyObject?' */
dfrib
  • 70,367
  • 12
  • 127
  • 192
MJQZ1347
  • 2,607
  • 7
  • 27
  • 49

2 Answers2

1

You can explicitly deal with larger numbers, even on 32 bit devices, if you explicitly specify UInt64 or Int64 (unsigned, and signed, respectively).

Alexander
  • 59,041
  • 12
  • 98
  • 151
1

(This answers the edited question: error when explicitly using the Int64 type)

Given your error message, it would seems as if the method .queryStartingAtValue(...) expects type AnyObject?, which will allow using, as argument, types automatically (implicitly) bridgeable to AnyObject, which explains why you don't have this issue with the Int type, whereas you do with the Int64 type.

I.e., the former (Int) is automatically bridged to a Obj-C/Cocoa class type (NSNumber) whereas this automatic bridging is not natively accessible for the latter (Int64).

There's two ways to redeem this

  1. Explicitly perform the bridging from Int64 to the equivalent NSNumber type, using the NSNumber initializer init(longLong: <Int64>)

    let foo: Int64 = 1465222757817
    let bar = NSNumber(longLong: foo)
    

    I.e., in your example, you could attempt the following:

    //...
        .queryStartingAtValue(NSNumber(longLong: timestampQueryValue))
    
  2. Or, using undocumented features (that might break in the future): conform Int64 to the internal protocol _ObjectiveCBridgeable, to allow the same implicit NSNumber bridging as is available for Int type. The following threads explains exactly this implementation:

    After implementing this implicit bridging for Int64, your existing code should work as is, as the Int64 argument to .queryStartingAtValue(...) will be automatically converted to the appropriate NSNumber (class) type.

Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192