在设备之间传输时数据丢失。怀疑它真的丢失了 - 我确信我做错了什么 - 但是什么?!
4步进近。前3个步骤就像魅力一样,我能够验证数据是否可以被解析(在发送之前)。 我收到了数据,但不是全部?在解析它时,我可以确认数据类型,但不能确认类别信息(性别,类别,子类别) - 为什么?步骤:
1。生成数据
func selectCategories(){
//Fixed data for testing
let gender = "Women"
let category = "Shoes"
let subCategory = "Pumps"
print("SENDING - Gender: \(gender), Category: \(category), SubCategory: \(subCategory)")
sendCategories(gender, category: category, subCategory: subCategory)
}
2。验证数据存储
func sendCategories(gender: String, category: String, subCategory: String){
var messageCategory = MessageCategory(message: Message(messageType: MessageType.Category), gender: gender, category: category, subCategory: subCategory)
let data = NSData(bytes: &messageCategory, length: sizeof(MessageCategory))
print("SEND - LENGTH: \(data.length)")
// VERIFY DATA STORED SUCCESFULLY (FOR TESTING PURPOSES ONLY)
let messageCategoryFromData = UnsafePointer<MessageCategory>(data.bytes).memory
if messageCategoryFromData.message.messageType == MessageType.Category{
print("SENDING - Gender: \(messageCategoryFromData.gender), Category: \(messageCategoryFromData.category), SubCategory: \(messageCategoryFromData.subCategory)")
let temp_gender = messageCategoryFromData.gender
let temp_category = messageCategoryFromData.category
let temp_subCategory = messageCategoryFromData.subCategory
let message = messageCategoryFromData.message
// SUCCESSFULLY PRINTS THE GENDER ETC WITH CORRECT INFORMATION -> DATA PARSING WORKS PRIOR TO SENDING DATA
print("RECEIVED (PRIOR TO SENDING). Gender: \(temp_gender) Category: \(temp_category) SubCategory: \(temp_subCategory)")
}
else{
print("WRONG MESSAGE TYPE")
}
// SEND THE DATA
sendData(data)
}
第3。发送数据
func sendData(data: NSData) {
print("SENDING DATA")
var sendDataError: NSError?
let gameKitHelper = GameKitHelper.sharedInstance
if let multiplayerMatch = gameKitHelper.multiplayerMatch {
do {
try multiplayerMatch.sendDataToAllPlayers(data, withDataMode: GKMatchSendDataMode.Reliable)
} catch let error as NSError {
print("Error while sending data: \(error)")
} catch {
print("Catch all errors: \(error)")
}
}
}
4。接收数据和分组信息(错误)
func matchReceivedData(match: GKMatch, data: NSData, fromPlayer player: String) {
var message = UnsafePointer<Message>(data.bytes).memory
if message.messageType == MessageType.Category {
let messageCategory = UnsafePointer<MessageCategory>(data.bytes).memory
print("MESSAGE: \(messageCategory.message)") <-- SUCCESSFUL
print("DATA TYPE: \(messageCategory.message.messageType)") <-- SUCCESSFUL
print("RECEIVED - LENGTH: \(data.length)") <-- SUCCESSFUL
let gender = messageCategory.gender <-- BECOMES NIL
let category = messageCategory.category <-- BECOMES NIL
let subCategory = messageCategory.subCategory <-- BECOMES NIL
delegate?.setCategories(g, category: c, subCategory: s)
print("RECEIVING - Gender: \(gender), Category: \(category), subCategory\(subCategory)")
}
}
支持结构
enum MessageType: Int {
case RandomNumber, Category, GameBegin, Move, LapComplete, GameOver
}
struct Message {
let messageType: MessageType
}
struct MessageCategory {
let message: Message
let gender: String
let category: String
let subCategory: String
}
答案 0 :(得分:0)
我稍微修改了你的代码
import XCPlayground
import Foundation
enum MessageType: Int {
case RandomNumber, Category, GameBegin, Move, LapComplete, GameOver
}
struct Message {
let messageType: MessageType
}
struct MessageCategory {
let message: Message
let gender: String
let category: String
let subCategory: String
}
func sendCategories(gender: String, category: String, subCategory: String)->NSData {
var messageCategory = MessageCategory(message: Message(messageType: MessageType.Category), gender: gender, category: category, subCategory: subCategory)
let data = NSData(bytes: &messageCategory, length: sizeof(MessageCategory))
print("SEND - LENGTH: \(data.length)")
// VERIFY DATA STORED SUCCESFULLY (FOR TESTING PURPOSES ONLY)
let messageCategoryFromData = UnsafePointer<MessageCategory>(data.bytes).memory
if messageCategoryFromData.message.messageType == MessageType.Category{
print("SENDING - Gender: \(messageCategoryFromData.gender), Category: \(messageCategoryFromData.category), SubCategory: \(messageCategoryFromData.subCategory)")
let temp_gender = messageCategoryFromData.gender
let temp_category = messageCategoryFromData.category
let temp_subCategory = messageCategoryFromData.subCategory
let message = messageCategoryFromData.message
// SUCCESSFULLY PRINTS THE GENDER ETC WITH CORRECT INFORMATION -> DATA PARSING WORKS PRIOR TO SENDING DATA
print("RECEIVED (PRIOR TO SENDING). Gender: \(temp_gender) Category: \(temp_category) SubCategory: \(temp_subCategory)")
}
else{
print("WRONG MESSAGE TYPE")
}
// SEND THE DATA
//sendData(data)
return data
}
let data = sendCategories("gender", category: "category", subCategory: "subcategory")
func matchReceivedData(data: NSData, fromPlayer player: String) {
var message = UnsafePointer<Message>(data.bytes).memory
if message.messageType == MessageType.Category {
let messageCategory = UnsafePointer<MessageCategory>(data.bytes).memory
print("MESSAGE: \(messageCategory.message)")
print("DATA TYPE: \(messageCategory.message.messageType)")
print("RECEIVED - LENGTH: \(data.length)")
let gender = messageCategory.gender
let category = messageCategory.category
let subCategory = messageCategory.subCategory
print("RECEIVING - Gender: \(gender), Category: \(category), subCategory: \(subCategory)")
}
}
matchReceivedData(data, fromPlayer: "player")
/*
SEND - LENGTH: 80
SENDING - Gender: gender, Category: category, SubCategory: subcategory
RECEIVED (PRIOR TO SENDING). Gender: gender Category: category SubCategory: subcategory
MESSAGE: Message(messageType: MessageType.Category)
DATA TYPE: Category
RECEIVED - LENGTH: 80
RECEIVING - Gender: gender, Category: category, subCategory: subcategory
*/
我的代码中没有“发送”。正如你所看到的,麻烦在于你'发送 - >接收'部分。
匹配参数是什么?它没有用在你的函数中..
func matchReceivedData(match: GKMatch, data: NSData, fromPlayer player: String)
答案 1 :(得分:0)
错误在于我使用 MERGE INTO <yourtable> base
USING ( <query above>
) new
ON ( new.rid = base.rid
and new.value = base.value
and new.original_keyword = base.keyword )
WHEN MATCHED THEN UPDATE
SET base.keyword = new.keyword
/
来处理数据。但在阅读之后,我意识到结构只是指向数据。这导致我的Struct
数据未与NSData一起发送(String
和Float
正常工作。)
解决方案是不使用struct来处理对象数据,而是将其作为原始NSData传递,然后总是包含一部分数据,它告诉它是哪种类型的数据,然后使用正确的解析。