简体   繁体   English

从静态库回调

[英]Callback from static library

I think this should be simple, but im having a real hard time finding information about this topic. 我认为这应该很简单,但是我很难找到有关此主题的信息。 I have made a static library and have no problem getting the basics to work. 我制作了一个静态库,让基础知识正常工作没有问题。 But im having a hard time figuring out how to make a call back from the static library to the main APP. 但是我很难弄清楚如何从静态库向主应用程序回叫。

I would like my static library to only use one header as front, this header should contain functions like: 我希望我的静态库仅将一个标头用作前端,此标头应包含如下功能:
requestImage:(NSString *)path; requestImage:(NSString *)path;
requestLikstOfSomething:(NSSting *)guid; requestLikstOfSomething:(NSSting *)guid;
and so on.. 等等..

These functions should do the necessary work and start a async NSURLConnection, and call back to the main application when the call have finished. 这些函数应完成必要的工作并启动异步NSURLConnection,并在调用完成后回调到主应用程序。 How do you guys do this, what are the best ways to callback from a static library when a async method is finished? 你们是如何做到的?异步方法完成后,从静态库回调的最佳方法是什么? should i do this with delegates (is this possible), notifications, key/value observers. 我应该用委托人(可能),通知,键/值观察者吗? I really want to know how you guys have solved this, and what you regard as the best practices. 我真的很想知道你们如何解决了这个问题,以及您认为什么是最佳实践。

Im going to have 20-25 different calls so i want the static library header file to be as simple as possible preferable only with a list of the 20-25 functions. 我将要有20-25个不同的调用,所以我希望静态库头文件尽可能简单,最好仅包含20-25个函数的列表。

UPDATE: 更新:
My question is not how to use delegate pattern, but witch way is the best to do callbacks from static librarys. 我的问题不是如何使用委托模式,但女巫的方式是从静态库中进行回调的最佳方法。 I would like to use delegates but i dont want to have 20-25 protocol declarations in the public header file. 我想使用委托,但我不想在公共头文件中有20-25个协议声明。 I would prefer to have only one function for each request. 我希望每个请求只有一个功能。

Solution choosen: 选择的解决方案:
i choose the solution from erkanyildiz with the help of a target parameter, i know its pretty low tech, but it was for me the cleanest solution. 我借助目标参数从erkanyildiz中选择解决方案,我知道它的技术水平很低,但这对我来说是最干净的解决方案。 My goal was to keep the header file as small as possible. 我的目标是使头文件尽可能小。 Thanks to everybody for they input, i will for sure look more into borrrdens solution with grand central dispatch when i get the time. 感谢大家的投入,我可以肯定的是,我有空的时候,我将在集中调度的基础上进一步研究borrrdens解决方案。 user1055604 solution with a couple of "standard" delegates for replys is also one i like. 我喜欢一个带有几个“标准”委托人答复的user1055604解决方案。 So again thank you all for inputs. 再次感谢大家的投入。

Thanks in advance. 提前致谢。 Best regards Morten 最好的问候莫滕

Look here for an example with delegates: How to perform Callbacks in Objective-C . 在此处查看有关委托的示例: 如何在Objective-C中执行回调

It shouldn't matter that the callback is executing from within a static library . callback是在static library执行的,这没关系。

Static libraries are a compile/link time phenomenon. 静态库是编译/链接时现象。 As such, calls to and from within static libraries are transparent to the application at runtime. 这样,在运行时,对静态库的调用和from within静态库的调用to应用程序是透明的。

Delegation pattern is a good way to do it. 委托模式是一种很好的方法。

You can check these pages: 您可以检查以下页面:

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18 http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18

http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example

As another approach, you can use a target parameter in every method you have. 另一种方法是,可以在每种方法中使用目标参数。 And schedule callbacks on those targets, checking if they are responding to your callbacks using respondsToSelector 然后在这些目标上安排回调,检查它们是否使用respondsToSelector响应您的回调

If you would rather not have one callback protocol declaration for each function in your static library, start with looking at what each callback would return if you did, forcefully, implement it as a callback. 如果您不想在静态库中为每个函数都使用一个回调协议声明,请先查看每个回调如果强制执行后将实现为回调的结果。

From there on, find what's in common between them and bind the common elements together in a class which will serve as an interface for the responses. 从那里开始,找到它们之间的共同点,并将共同的元素绑定到一个类中,该类将用作响应的接口。 You may need more that one such class. 您可能需要一个以上的此类课程。

As an example, look at NSURLResponse : 例如,看一下NSURLResponse


NSURLResponse declares the programmatic interface for an object that accesses the response returned by an NSURLRequest instance. NSURLResponse为访问NSURLRequest实例返回的响应的对象声明编程接口

NSURLResponse encapsulates the metadata associated with a URL load in a manner independent of protocol and URL scheme. NSURLResponse以与协议和URL方案无关的方式封装与URL负载关联的元数据。

This class is used by NSURLConnection in its didReceiveResponse delegate method. NSURLConnection在其didReceiveResponse委托方法中使用此类。 In the worse case, you will end up with a couple of callbacks instead of a noisy header file. 在最坏的情况下,您将得到几个回调,而不是一个嘈杂的头文件。 Happy coding. 快乐的编码。 :-) :-)

Try making your function calls using Grand Central Dispatch. 尝试使用Grand Central Dispatch进行函数调用。 It's quite simple, and very powerful. 这非常简单,而且功能非常强大。 Here is a sample of how to make an async call with a callback: 以下是如何通过回调进行异步调用的示例:

http://pastebin.com/xDUKm6wh http://pastebin.com/xDUKm6wh

This paste bin is complaining about errors, but take note of the pattern. 此粘贴容器抱怨错误,但请注意该模式。 It should work just fine. 它应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM