Post

Replies

Boosts

Views

Activity

Reply to Fragment large size data sent and received using NSKeyedArchiver.archivedData in GameCenter
[quote='796608022, DTS Engineer, /thread/759890?answerId=796608022#796608022'] Finally, how is MCPeerID coming into this? I thought you were using GameKit? [/quote] Actually, I am using both GameKit and MCSession. Trying to play with fragmenting the data, as you prompted. Although it is more or less clear how to send data: func sendData(dictionaryWithData dictionary: Dictionary<String, Any>, toPeer targetPeers: [MCPeerID]) { do { let dataToSend = try NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: true) let fragments = fragmentsForMessage(dataToSend, messageID: 0xa1a2a3a4, maxMessageBytesInFragment: 50000) for fragment in fragments { try session.send(fragment, toPeers: targetPeers, with: MCSessionSendDataMode.reliable) } } catch { } } ... I am frankly a bit stuck as to how to handle it in func session(_ session: MCSession,didReceive data: Data,fromPeer peerID: MCPeerID) { DispatchQueue.main.async { do { .... Append data to a property and then at some point implement NSDictionary.unsecureUnarchived? How to know when to implement? When reading the data, I am also getting errors Error:Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0xffffffa1, 0xffffffa2, 0xffffffa3, 0xffffffa4, 0x0, 0x0, 0x0, 0x6)" UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0xffffffa1, 0xffffffa2, 0xffffffa3, 0xffffffa4, 0x0, 0x0, 0x0, 0x6)}
Jul ’24
Reply to Fragment large size data sent and received using NSKeyedArchiver.archivedData in GameCenter
Thank you so much. I also did notice that the size is unreasonably large. For example, an array of 52 elements of a custom object Card with the following init methods takes about 70KB: required init?(coder aDecoder: NSCoder) { self.suit = Suit(rawValue: aDecoder.decodeInteger(forKey: "suit"))! // (custom enum) self.rank = Rank(rawValue: aDecoder.decodeInteger(forKey: "rank"))! // (custom enum) self.positionX = CGFloat (aDecoder.decodeDouble(forKey: "positionX")) self.positionY = CGFloat (aDecoder.decodeDouble(forKey: "positionY")) self.frontTexture = aDecoder.decodeInteger(forKey: "frontTexture") self.backTexture = SKTexture(imageNamed: "card_back") self.faceUp = aDecoder.decodeBool(forKey: "faceUp") self.possession = aDecoder.decodeObject(forKey: "possession") as! String self.possessionPrevious = aDecoder.decodeObject(forKey: "possessionPrevious") as! String self.notDumpable = aDecoder.decodeBool(forKey: "notDumpable") self.intersectsDeck = aDecoder.decodeInteger(forKey: "intersectsDeck") super.init(coder: aDecoder) } override func encode(with aCoder: NSCoder) { super.encode(with: aCoder) aCoder.encode(suit.rawValue, forKey: "suit") aCoder.encode(rank.rawValue, forKey: "rank") aCoder.encode(Double(positionX), forKey: "positionX") aCoder.encode(Double(positionY), forKey: "positionY") aCoder.encode(frontTexture, forKey: "frontTexture") aCoder.encode(faceUp, forKey: "faceUp") aCoder.encode(possession, forKey: "possession") aCoder.encode(possessionPrevious, forKey: "possessionPrevious") aCoder.encode(notDumpable, forKey: "notDumpable") aCoder.encode(intersectsDeck, forKey: "intersectsDeck") } So, I did consider sending data using property list encoding and decoding. However, I read that it is not possible to use it to encode/decode Dictionary [String:Any], which I need to send (e.g., https://stackoverflow.com/questions/53585848/swift-encode-and-decode-a-dictionary-stringany-into-plist). Is this true? Another option may be to use JSONSerialization, but I am not sure whether it is significantly smaller than NSKeyedArchiver.
Jul ’24