Communication between the daemon and frontend processes of iPhone Development

Source: Internet
Author: User

IPhone DevelopmentDaemonProcessAnd front-endProcessBetweenCommunicationThis is the content to be introduced in this article. When we create an application based on mobilesubstrate, there are usually some frontend and backend programs, such as apple skins. This requires foreground procedural code information to go to the background.Process, There are two methods

One is file-based mode.

That is, set a timer in the background program to regularly read the file of user interaction information. This communication mechanism also solves the problem. However, the defect is that you need to run a timer all the time to check whether the front-end has passed the information.

Another method is to use CFMessagePortRef.

The typical mode is as follows:

 
 
  1. #define APP_ID "yohunl.support.mach.port"  
  2. #define MACH_PORT_NAME APP_ID 

Create a CFMessagePortRef for Process Communication in the background process

 
 
  1. CFMessagePortRef local = CFMessagePortCreateLocal(kCFAllocatorDefault, 
  2. CFSTR(MACH_PORT_NAME), mouseCallBack, NULL, NULL);  
  3.   CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, 0);  
  4. CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes); 

The mouseCallback is the callback function, and its declaration is

 
 
  1. CFDataRef mouseCallBack(CFMessagePortRef local, SInt32 msgid, CFDataRef cfData, void *info); 

At the front-endProcessUse the message sending mode in

 
 
  1. CFMessagePortRef bRemote = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR(MACH_PORT_NAME));  
  2. // tell thread b to print his name  
  3. char message[255]="lingdaiping,yohunl";  
  4. CFDataRef data;  
  5. data = CFDataCreate(NULL, (UInt8 *)message, strlen(message)+1);  
  6. (void)CFMessagePortSendRequest(bRemote, CFSTR(MACH_PORT_NAME), data, 0.0, 0.0, NULL, NULL);  
  7. CFRelease(data);  
  8. CFRelease(bRemote); 

There is also a semaphore mechanism that I have not studied yet, but I have seen other programs used, and it should be OK!

Summary:IPhone DevelopmentDaemonProcessAnd front-endProcessBetweenCommunicationI hope this article will be helpful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.