Friday, May 6, 2011

Can someone show me a diagram of how view controllers work?

Can someone show me a diagram of how view controllers work in cocoa (obj-c).

I just want to understand because they are confusing me

Thanks!

From stackoverflow
  • I don't think a UIViewController can be summed up in a diagram.

    What specifically is it that you are having difficulty with? The navigation from one controller to another? The stacking aspect of a NavigationController? Or the structure of your look and feel inside a ViewController?

    The question is so open ended, I don't think it can be answered in a single thread on this site. I would point you toward Lecture 6 of the Stanford class on iPhone programming. Both the lecture notes and the lecture itself (with sample exercises) can be found at: http://www.stanford.edu/class/cs193p/cgi-bin/index.php

    Daniel Kindler : thanks, I'll watch that video. But I'm just having trouble understanding what is it that the view-controller does?
    danielpunkass : Agree with mmc. You should try to seek out more broad-spectrum explanations of iPhone programming in general, then return with a more specific question if you still don't understand.
  • The stanford link is a great reference.

    Generally, a view controller provides the "glue" to your application. It should get/process the data from your Model(s) and hand it off to the view. Almost all of the application logic will be in the View Controller.

    The following text is from the Cocoa Fundamentals Guide by Apple:

    Controller Objects Tie the Model to the View

    A controller object acts as the intermediary between the application's view objects and its model objects. Controllers are often in charge of making sure the views have access to the model objects they need to display and act as the conduit through which views learn about changes to the model. Controller objects can also perform set-up and coordinating tasks for an application and manage the life cycles of other objects.

    In a typical Cocoa MVC design, when users enter a value or indicate a choice through a view object, that value or choice is communicated to a controller object. The controller object might interpret the user input in some application-specific way and then either may tell a model object what to do with this input—for example, "add a new value" or "delete the current record"; or it may have the model object reflect a changed value in one of its properties. Based on this same user input, some controller objects might also tell a view object to change an aspect of its appearance or behavior, such as disabling a button. Conversely, when a model object changes—say, a new data source is accessed—the model object usually communicates that change to a controller object, which then requests one or more view objects to update themselves accordingly.

    Controller objects can be either reusable or non-reusable, depending on their general type. “Types of Cocoa Controller Objects” describes the different types of controller objects in Cocoa.

    A relevant diagram would be the relationship between the Model,View, and the Controller (again courtesy of apple):

    MVC Diagram

    Daniel Kindler : thanks man! that waas a help

Rails/Passenger/Unknown Content Type

We have the following situation:

  1. We invoke a url which runs an action in a controller. The action is fairly long running - it builds a big string of XML, generates a PDF and is supposed to redirect when done.

  2. After 60 seconds or so, the browswer gets a 200, but with content type of "application/x-unknown-content-type" no body and no Response Headers (using Tamper to look at headers)

  3. The controller action actually continues to run to completion, producing the PDF

This is happening in our prod environment, in staging the controller action runs to completion, redirecting as expected.

Any suggestions where to look?

We're running Rails 2.2.2 on Apache/Phusion Passenger.

Thanks,

From stackoverflow
  • I am not 100% sure, but probably your Apache times out the request to Rails application. Could you try to set Apache's Timeout directive higher? Something like:

    Timeout 120
    
  • I'd consider bumping this task off to a job queue and returning immediately rather than leaving the user to sit and wait. Otherwise you're heading for a world of problems when lots of people try to use this and you run out of available rails app instances to handle any new connections.

    One way to do this easily might be to use an Ajax post to trigger creating the document, drop this into Delayed Job and then run a 10 second periodic check via ajax informing the waiting user of the jobs status. Once delayed_job has finished processing your task in the background and updated something in the database to indicate it is complete, then you can redirect the user via ajax to the newly created document.

Do we really need NULL?

Is there an actual need for NULL or not? In most of the OO languages i have programmed in there has always been a way to set a variable to a NULL value which lead to all sorts of funny problems.

What are your thoughts?

From stackoverflow
  • It's possible to design a language that doesn't have a NULL but instead uninitialised values point to a singleton dummy object that doesn't actually do anything. You could compare pointers against the reference of this dummy object, and calls to methods on the object would result in no action or a runtime error.

    This technique is hard to implement for statically typed languages like C++ or Java.

    Drew Hoskins : Yeah, apparently (dynamically-typed) Objective-C does nothing when you call a method on a nil object, though it does have nil.
  • NULL is a little like God. If it didn't exist, we would wind up having to create one. Something has to represent the value of a reference that is unassigned (whether that be because it was never assigned or it was cleared at some point). The only alternative is to use an object that, effectively, substitutes for NULL. The problem with that is that if you did all that to avoid the NullPointerException, now you're going to simply replace it with UnexpectedObject exception or ClassCastException or what not.

    Charlie Martin : Definitely +1 for the Voltaire reference.
    Miky Dinescu : +1 ha ha.. that's sort of funny, but true I guess..
    Daniel Earwicker : NULL is a little like man also. For God (Anthony Hoare) created man (NULL) and then a few years later wished he hadn't.
  • In languages with garbage collection where variables are actual storage locations (as opposed to Python's labels), the NULL value is required to allow memory to be freed in a clean manner before the end of the variable's scope.

    Also, even many algorithms written in pseudo code make use of the special NULL value. It pops up literally everywhere. It is a central concept in computer science.

  • There are normally two special values. Some languages handle both, some only 1 and throw an error with the other, and some merge the two. Those two values are Null and Undefined.

    • Undefined would be trying to use a variable that flat out doesn't exist.

    • Null would be trying to use a variable that exists but has no value.

    Null can be useful because it is a guaranteed value that indicates that something is wrong, or outside of the domain/range of possible answers. Take Java for instance:

    If you did not have null, what if you did a lookup in a HashMap for something that didn't exist in the Map? What would you return? If you returned an Object then how would you know wether that Object meant nothing was there or this was what was actually in the Map. Workarounds could include creating your own "NON_EXIST" Constant Object, but thats essentially the same thing as what null already is anyways. Another workaround might be throwing an Exception. Now you're looking at major performance impacts.

    Again, its the idea of having a guaranteed allowable value that you can use. It is always available to you and its always disjoint from the set of "real values" that would normally return from an operation you're performing. Null is therefore intentionally used to mean something special.

    Like I hinted before, there are also optimizations that can be done here as well because Null is a special value. In languages like Java if you originally referenced an Object solely by a variable, then set that variable to null you're removing that reference and allowing Java's Garbage Collector to collect that now unreferenced data. If instead you let that variable sit around forever that hunk of memory that may never be used again will continue to hold resources. This is a contrived example, but it proves a point and in some resource intensive Java programs you will see these explicit assignments to null.

  • my answer to that question is NULL

Hyper-Threading programming languages

What languages can hyper-threading be implemented in? Is it only part of Object Oriented systems or can it be implemented in C?

Thanks.

From stackoverflow
  • Any language and runtime that supports threads will support hyperthreading.

    Hyper-threading is a way of multiplexing a CPU between multiple threads - there is only one real CPU but it is visible to the operating system as two CPU's, and thus two threads can be scheduled on it. Any stalls in the CPU on one thread (like waiting on memory, long FPU operations, etc.), allow the CPU to execute code from the other thread.

    More info on hyper-threading at Wikipedia.

    Drew Hoskins : It's actually finer-grained even than that. Different computational units (arithmetic, floating point) can be simultaneously computing results of instructions from different threads.
    Michael : Yeah ... a more complete answer would give this in the context of out-of-order execution and pipeline stalls but I didn't want to compress the 700 pages of Hennessy and Patterson into an answer here :)
    Michael : Actually, I'm not sure if different computational units simultaneously is possible in the case of the Atom since it is an in-order part.

Tying tables together in MySql

I have one table that includes company information (i.e. name, address, business description...) and another table that has user information (i.e. first name, last name, email and password). How do I tie the company table to the user table so that when the user logs in, the company is properly associated with the user? Would I have a userid column in the company table?

From stackoverflow
  • You are looking for joins.

    Companies would have a one-to-many relationship with the users, so you need to create a column in the user table with a name like company_id and then query it like this to get users and their company:

    SELECT * FROM users INNER JOIN companies ON users.company_id = companies.id
    
    Paolo Bergantino : :? How is this wrong? I'm rereading the question and this is what he's asking about.
    BrynJ : Agree, I'm not sure why your answer would be down-voted
    Adrien : +1 From me ... Also, see http://stackoverflow.com/questions/832322/question-about-joining-two-mysql-tables/
  • Assuming that several users might belong to a single company but only one company may be associated with a user, a companyid in the user table would make the most sense.

    To join two tables together where you expect a match between both in all cases, use an INNER JOIN:

    SELECT u.*, c.companyname FROM users u 
    INNER JOIN companies c ON u.companyid = c.companyid
    

    Obviously, the above is a simulated query and needs tailoring to match your schema / requirements.

  • No, you will probably have a one-to-many relation. So one company contains many employees. But one employee belongs to only one company (to keep things simple).

    This requires a foreign key in the user table pointing to the id of the company.

SubSonic doesn't map stored procedure in packages

Ive Used Subsonic for MSSQL and works wonderfull!!!! Does anyone know whats with oracle's SPS in Packages? Please help. need to deliver a func verry soon.

From stackoverflow
  • Oracle packages aren't supported. I'm not sure there are plans to support them any time soon.

Web server - when should I use chunked transfer encoding ?

Looking at various web servers HTTP Headers, I notice that Google.com has:

client-transfer-encoding: "chunked"

What is chuncked transfer encoding and should I be using it on my web server?

From stackoverflow
  • Chunked can be used to send a HTTP request or response in multiple parts, and send one part while subsequent parts are not available.

    Multiple request--response pairs can be transferred over a single HTTP connection. (This is to avoid the overhead of the TCP connect() for subsequent requests.) To implement this, the client needs to know where the server response ends. If the server generates the Content-Length header, the client can count down the bytes. When there are no bytes left to read, the client can initiate the next request. But how would the server generate the Content-Length header if it doesn't know the length of the full response in advance? The solution is to use chunked instead of Content-Length.

    Apache (1.3 and 2), by default, sends static files as chunked whenever it makes sense (and the HTTP client supports it). You don't have to take any action. If you write your own web application, you might consider generating a chunked response by hand.

    See http://www.research.att.com/~bala/papers/h0vh1.html and http://developers.sun.com/mobility/midp/questions/chunking/ for a little more.

    Emil H : How does this relate to http 1.1 keep-alives? Are they basicly the same thing, or can a web server send chunked respones even if it doesn't support keep-alives?
    pts : Chunked can be used without keep-alive, but this doesn't make sense, because chunked without keep-alive doesn't provide any benefit.
    meandmycode : pts I don't think thats correct, a chunk needs to be sent in a single connection otherwise there would be no context in what a chunk is in relation two.. imagine a server receiving tons of connections starting with chunks (which are just a hex length, crlf then payload).