2016-09-15 5 views
26

के साथ कनेक्टिविटी मुद्दे देखें मेरी परियोजना में मैं घड़ी और आईफोन से संदेश भेजने के लिए Watch Connectivity का उपयोग करता हूं। मैं फोन पर एक संदेश भेज सकता हूं और ऐप लॉन्च करते समय तारों की एक सरणी प्राप्त कर सकता हूं, हालांकि क्रियाओं का उपयोग करते समय मुझे निम्न त्रुटि मिलती है;"संदेश उत्तर बहुत लंबा लिया।" - वॉच ओएस 3

त्रुटि डोमेन = WCErrorDomain कोड = 7012 "संदेश उत्तर बहुत लंबा लगा।"

यहां बताया गया है कि चीजें कैसे स्थापित की जाती हैं;

सबसे पहले घड़ी फोन पर एक संदेश भेजती है और फिर फोन WKInterfaceTable में प्रदर्शित करने के लिए तारों की एक सरणी भेजता है। यह कभी-कभी ऐप लोड करते समय काम करता है। (मैं सभी NSManagedObjects Items कहा जाता है और एक arraywatchItems कहा जाता है में स्टोर करने के लिए उनके title स्ट्रिंग गुणों का उपयोग लाने।

हालांकि मैं घड़ी पर एक कार्रवाई सरणी के सभी आइटम हटाना और नए डेटा के साथ तालिका को ताज़ा करने के लिए है।

घड़ी पर कार्रवाई फ़ंक्शन का उपयोग item को सरणी से हटाने के लिए फ़ोन पर भेजने के लिए करती है, फिर फ़ोन घड़ी में नई अपडेटेड सरणी भेजता है और घड़ी तालिका को अद्यतन करता है। हालांकि मुझे या तो एक ही सरणी मिलती है पीछे या एक त्रुटि।

बहुत आसान सही, तो कभी स्विफ्ट 3 और वॉच ओएस 3/आईओएस 10 से पहले वास्तव में ठीक काम किया; पूरा ऐप काम करने के लिए इस्तेमाल किया।

यहां बताया गया है कि मेरे पास सब कुछ कैसे स्थापित है;

फ़ोन अनुप्रयोग प्रतिनिधि

import WatchConnectivity 

class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate { 

var session : WCSession! 

var items = [Items]() 

func loadData() { 
    let moc = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext 
    let request = NSFetchRequest<Items>(entityName: "Items") 

    request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)] 
    request.predicate = NSPredicate(format: "remove == 0", "remove") 

    do { 
     try 
      self.items = moc!.fetch(request) 
     // success ... 
    } catch { 
     // failure 
     print("Fetch failed") 
    } 
} 

//WATCH EXTENSION FUNCTIONS 
//IOS 9.3 
/** Called when the session has completed activation. If session state is WCSessionActivationStateNotActivated there will be an error with more details. */ 


//HAVE TO INCLUDE 
@available(iOS 9.3, *) 
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?){ 
    print("iPhone WCSession activation did complete") 
} 


@available(iOS 9.3, *) 
func sessionDidDeactivate(_ session: WCSession) {} 

func sessionWatchStateDidChange(_ session: WCSession) {} 

func sessionDidBecomeInactive(_ session: WCSession) { 

} 

//APP DELEGATE FUNCTIONS 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 

    //Check if session is supported and Activate 
    if (WCSession.isSupported()) { 
     session = WCSession.default() 
     session.delegate = self; 
     session.activate() 
    } 
    return true 
} 


} 

//DID RECIEVE MESSAGE 
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) { 


    loadData() 

    func loadItems() { 
     watchItems.removeAll() 

     for a in self.items { 
      watchItems.append(a.title) 
     } 
    } 

    var watchItems = ["1","2","3","4","5"] 

    let value = message["Value"] as? String 

    //This is called when user loads app, and takes some time when using refresh action, sometimes times out 

    if value == "HELLOiPhone/[email protected]=" { 

     print("Hello Message Recieved") 

     loadItems() 

     //send a reply 
     replyHandler([ "Items" : Items ]) 

    } 

    //Not sure if receiving but does not delete array and send back to watch 
    if value == "[email protected]+=-/" {       
     for index in self.items { 
      index.remove = 1 
      //Saves MOC 
     } 

     loadData() 
     loadTasksData() 

     //send a reply 
     replyHandler([ "Items" : Items ]) 

    } 
    else { 
     for index in self.items { 
      if index.title == value { 
      index.remove = 1 
      //Saves MOC 
      } 
     } 

     loadData() 
     loadTasksData() 

     //send a reply 
     replyHandler([ "Items" : Items ]) 
    } 
} 

वॉच

import WatchConnectivity 

class SimplelistInterfaceController: WKInterfaceController, WCSessionDelegate { 


/** Called when the session has completed activation. If session state is WCSessionActivationStateNotActivated there will be an error with more details. */ 
@available(watchOS 2.2, *) 
public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { 

    //Fetch data is a function which sends a "HELLOiPhone/[email protected]=" message to receive the array and displays in the table. This works 
    fetchData() 
} 


var session : WCSession! 
var items = ["Refresh Items"] 

override func didAppear() { 
    fetchData() 
} 

override func willActivate() { 
    // This method is called when watch view controller is about to be visible to user 
    super.willActivate() 
    //Check if session is supported and Activate 
    if (WCSession.isSupported()) { 
     session = WCSession.default() 
     session.delegate = self 
     session.activate() 
    } 
    fetchData() 
} 

override func awake(withContext context: Any?) { 
    super.awake(withContext: context) 
    fetchData() 
} 

@IBAction func refresh() { 
    print("Refresh") 
    //Works but sometimes message is delayed 
    fetchData() 
} 

@IBAction func removeAll() { 
    print("Remove All Items is called") 
    if WCSession.default().isReachable { 
     let messageToSend = ["Value":"[email protected]+=-/"] 
     print("\(messageToSend)") 
     session.sendMessage(messageToSend, replyHandler: { replyMessage in 
      if let value = replyMessage["Items"] { 
       self.items = value as! [String] 

       Not receiving message 
       print("Did Recieve Message, items = \(self.items)") 
      } 

      }, errorHandler: {error in 
       // catch any errors here 
       print(error) 
     }) 
    } 
    fetchData() 
} 

} 
+0

'WCSession' API केवल संपत्ति सूची प्रकारों के साथ शब्दकोश लेता है, लेकिन मुझे लगता है कि आप 'आइटम' भेज रहे हैं। ये ऑब्जेक्ट्स क्या हैं, और क्या आप वाकई WCSession API द्वारा समर्थित हैं? – ccjensen

+0

@ccjensen आइटम वास्तव में एक 'NSManagedObject' है, हालांकि मैं उन्हें लाने और घड़ी को भेजने से पहले, एक सरणी बनाने के लिए स्ट्रिंग मान के अपने शीर्षक गुणों का उपयोग करता हूं। हालांकि घड़ी पर प्रत्येक fetch के लिए, फोन डेटा अद्यतन और refetch माना जाता है। यह नहीं हो रहा है और अक्सर त्रुटि प्राप्त होती है। – JUSDEV

+0

डेटा प्रदर्शित करते समय वास्तव में घड़ी ठीक होती है, हालांकि ऑब्जेक्ट को हटाने का प्रयास करते समय प्रतिक्रिया नहीं होती है। – JUSDEV

उत्तर

0

मैं बस अपना WatchOS अनुप्रयोग के साथ पेश किया है। और एक स्थिति थी जब मुझे "संदेश जवाब बहुत लंबा लगा"।

फिर मैंने अपने आईओएस ऐप में पृष्ठभूमि कार्य हैंडलर जोड़ा और वॉचोस ऐप पर हर सेकेंड संदेश भेजना शुरू कर दिया। संदेश में UIApplication.shared.backgroundTimeRemaining शामिल था। तो मुझे मिलता है: 45sec, 44sec, ..., 6sec, 5sec, ... यदि टाइमर 5 सेकंड से नीचे चलता है तो कोई संदेश आईओएस ऐप से/नहीं दिया जाएगा और हमें "संदेश उत्तर बहुत लंबा लगेगा"। सबसे आसान समाधान घड़ी से फोन पर रिक्त संदेश भेजने के लिए प्रत्येक बार टाइमर 15 सेकंड से नीचे चला जाता था। backgroundTimeRemaining 45 सेकंड फिर से अपडेट किया जाएगा: 45, 44, 43, ..., 17, 16, 15, (खाली संदेश), 45, 44, 43, ...

आशा है कि यह किसी

में मदद करता है
संबंधित मुद्दे