2016-05-10 14 views
16

हम मूल रूप से प्रतिक्रिया करने के लिए मेरे आवेदन के क्रमिक रूपांतरण के माध्यम से जा रहे हैं। और मैं आईओएस पर प्रतिक्रिया मूल में निर्भरता इंजेक्शन के साथ समस्याओं में पड़ रहा हूं।प्रतिक्रियात्मक मॉड्यूल में निर्भरता इंजेक्शन

मेरे पास मेरे ऐप में कुछ सेवाएं हैं जो मैं मूल मॉड्यूल में उपयोग करना चाहता हूं। वर्तमान में, उन्हें टाइफून के माध्यम से इंजेक्शन दिया जाता है और सब कुछ ठीक काम करता है।

हालांकि, मूल प्रतिक्रिया स्वयं एकल मॉड्यूल को एकलटन के रूप में प्रारंभ और रखरखाव करता है। इससे मुझे टाइफून उन्हें प्रारंभ करने से रोकता है, और इसलिए मैं उन पर निर्भरता इंजेक्ट नहीं कर सकता।

क्या किया जा सकता है? आरसीटीब्रिज बनाना स्वयं एक विकल्प है, लेकिन बहुत कम स्तर लगता है, और अभी भी यह पता लगाने की आवश्यकता है कि इसे पहले स्थान पर UIView में कैसे इंजेक्ट करना है।

उत्तर

10

मुझे बिल्कुल यकीन नहीं है कि आपके प्रश्न को अधिक वोट क्यों नहीं मिला; मैं खुद एक ही सवाल का जवाब देने के लिए संघर्ष कर रहा था और सोचा था कि मुझे अपने पहले स्टैक ओवरफ्लो प्रश्न पर हॉप करना चाहिए और जवाब देना चाहिए!

RCTBridge class के आसपास खोदने का उत्तर प्रदान किया गया। आपको क्या करने की जरूरत है एक वर्ग है कि RCTBridgeProtocol (और महत्वपूर्ण बात यह विधि 'extraModulesForBridge' लागू करता है का एक उदाहरण के साथ मैन्युअल रूप से एक RCTBridge आरंभ है, तुम भी अपने दृश्य नियंत्रक में इस प्रोटोकॉल को लागू कर सकता है, जब आप

// Initialise a class that implements RCTBridgeDelegate 
// Be warned, RCTBridge won't store a strong reference to your delegate. 
// You should there store this delegate as a property of your initialising class, or implement the protocol in the View Controller itself. 
id<RCTBridgeDelegate> moduleInitialiser = [[classThatImplementsRCTBridgeDelegate alloc] init]; 

// Create a RCTBridge instance 
// (you may store this in a static context if you wish to use with other RCTViews you might initialise. 
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:nil]; 

// Initialise an RCTRootView 
RCTRootView *rootView = [[RCTRootView alloc] 
        initWithBridge:bridge 
         moduleName:kModuleName 
        initialProperties:nil]; 

// Note that your class that implements RCTBridgeDelegate SHOULD implement the following methods and it might look something like this. 

// This will return the location of our Bundle 
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge 
{ 
    return [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"]; 
} 

// Importantly, this is the method we need to implement to answer this question 
// We must return an array of initialised Modules 
// Be warned, I am writing this off of the top of my head, so excuse any syntax errors there might be! 
- (NSArray *)extraModulesForBridge:(RCTBridge *)bridge 
{ 
    return @[[OurNativeModule alloc] initWithCustomInitialiser:customDependency]]; 
} 

संपादित करें: मैं प्रतिक्रिया देशी प्रलेखन को यह जोड़ा https://facebook.github.io/react-native/docs/native-modules-ios.html#dependency-injection

+0

मैं वास्तव में अपने दम पर इस खोज की, लेकिन जब से हमारे पूरे एप्लिकेशन स्विफ्ट आधारित है स्विफ्ट का उपयोग करते समय RCTModules बनाने के लिए एक अच्छा तरीका है (मुझे लगता है कि यह RCTModule था, यह कुछ समय पहले किया गया था) नहीं मिल सकता है, । हालांकि, मैं इसे फिर से देख सकता हूं और नलसाजी के लिए केवल उद्देश्य सी का उपयोग कर सकता हूं और वास्तविक मूल मॉड्यूल को तेज़ी से लिख सकता हूं। क्या आप इसे काम कर रहे थे? – Rubys

+0

क्या मेरे मूल कार्यान्वयन के साथ डेटपीकर आईओएस जैसे मानक देशी मॉड्यूल को प्रतिस्थापित करने की कोई क्षमता है जो पिकर रंग बदलने के लिए निजी एपीआई की तरह कुछ उपयोग करता है? – pinguinjkeke

2

कोड ऊपर बस ठीक काम करता है यहाँ कोड की स्विफ्ट 4 संस्करण है

@objc(RNModuleInitialiser) 
final class RNModuleInitialiser: NSObject { 

    //Inject your dependencies here 
    init() { 

    } 

} 

extension RNModuleInitialiser: RCTBridgeDelegate { 

    func sourceURL(for bridge: RCTBridge!) -> URL! { 
     return URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")! 
    } 

    func extraModules(for bridge: RCTBridge!) -> [RCTBridgeModule]! { 

     var extraModules = [RCTBridgeModule]() 

     //Initialise the modules here using the dependencies injected above 

     return extraModules 
    } 

} 

जब पुल आरंभ, मी गुजरती हैं।।। oduleInitialiser:

//Make sure to hold the strong reference to the moduleInitaliser 

self.moduleInitialiser = RNModuleInitialiser() 
self.bridge = RCTBridge(delegate: self.moduleInitialiser, launchOptions: nil) 
संबंधित मुद्दे